P
P
Pavel Inky2016-07-19 17:34:12
PHP
Pavel Inky, 2016-07-19 17:34:12

Why do we need a final class?

Hi all.
Can you please tell me why final class is needed? Methods, I understand why make final - so that they would not be redefined. And here are the classes from which it is impossible to inherit, why do it? As I understand it, then unnecessary entities just weigh? Or I'm wrong? Please tell me, and if possible, examples of where to use it.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dev400, 2016-07-19
@dev400

I use database connections in the class, because it makes no sense to inherit this class. The connection is initialized and further work goes in the models

<?php
namespace app\Common\Mysql;

final class Connection
{

    protected $link;

    public function __construct() {

        if ( is_null($this->link) ) {
            try {
                $attr  = [
                    \PDO::ATTR_ERRMODE  =>  \PDO::ERRMODE_EXCEPTION,
                    \PDO::ATTR_DEFAULT_FETCH_MODE   =>  \PDO::FETCH_ASSOC
                ];

                $this->link = new \PDO("mysql:host=localhost;dbname=dbname;charset=utf8", "user", "pass", $attr);

            } catch (\PDOException $e) {
                file_put_contents('log.txt', $e->getMessage(), FILE_APPEND);
            }
        }
    }

    /**
     *  @return \PDO;
     */
    public function link() {
        return $this->link;
    }
}

<?php
namespace app\Common\Model;

use app\Common\Mysql\Connection;

abstract class BaseModel {

    private $connection;

    /**
     * prefix for tables
     */
    const PREFIX =  "";

    /**
     * @var
     */
    protected $table;

    /**
     * @var
     */
    protected $key;

    public function __construct(Connection $conn) {

        $this->connection = $conn->link();

    }

    public function findAll() {

        return $this->fetch( "SELECT * FROM " . self::PREFIX . $this->table );

    }

    /**
     * @param array $args
     * @param $sql
     * @return array|string
     */
    public function findBy(Array $args, $sql) {

        $stmt = $this->connection->prepare($sql);
        $data = "";
        if ( $stmt->execute($args) ) {
            while ($row = $stmt->fetch()) {
               $data[] = $row;
            }
        }
        return $data;

    }


    /**
     * @param $query
     * @return mixed
     */
    protected function fetch($query) {

        $stmt = $this->connection->query($query);
        $stmt->setFetchMode(\PDO::FETCH_ASSOC);
        $data = "";
        while ( $row = $stmt->fetch() ) {
            $data[] = $row;
        }
        return $data;

    }

    protected function save(Array $array, $sql) {

        $sth = $this->connection->prepare($sql);
        return $sth->execute($array);

    }

    protected function deleteById($id) {
        $id = (int)$id;
        return $this->connection->exec('DELETE FROM ' . self::PREFIX . $this->table . ' WHERE id = '.$id.'');
    }

}

D
Denis, 2016-07-19
@prototype_denis

class Foo
{
    const FOO_CONSTANT = 'foo';
}
class Bar extends Foo
{
    const FOO_CONSTANT = 'bar';
}
var_dump(Bar::FOO_CONSTANT); // bar



final class Biz
{
    const BIZ_CONSTANT = 'biz';
} 
// PHP Fatal error:  Class Baz may not inherit from final class (Biz)
// class Baz extends Biz {
//         
// }

A
AlikDex, 2016-07-20
@AlikDex

Not from the PHP world, but still. This is how viruses work without the word final in security-key classes:
https://habrahabr.ru/post/235713/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question