PHP: abstract class A { public function __construct() { var_dump(static::class, get_called_class(), get_class($this)); // B, B, B } } class B extends A { } new B(); А что сейчас в тренде так сказать, что использовать?
Лучше ничего. Родителю должно быть пофиг на потомков. --- Добавлено --- В тренде минимальное использование наследования
есть необходимость использовать это Спойлер: ВОТ ТУТ PHP: <?php namespace core; use PDO; abstract class ActiveRecord { private static $PDO; public $id; public $originalProperties = []; function __construct(PDO $PDO) { self::$PDO = $PDO; $this->originalProperties = $this->getProperties(); } private function getProperties() { return array_filter(get_object_vars($this), function ($property) { return !is_array($property); }); } public function count() { $table = $this->getTableName(); return $this->query("select count(*) from $table")->fetchColumn(); } abstract protected function getTableName(); public function query($sql, $params = [], $class = '') { $sth = self::$PDO->prepare($sql); $sth->execute($params); return $class ? $sth->fetchAll(PDO::FETCH_CLASS, $class, [self::$PDO]) : $sth; } public function countBy($params, $values = []) { $table = $this->getTableName(); return $this->query("select count(*) from $table where $params", $values)->fetchColumn(); } public function findOne($id) { $table = $this->getTableName(); return current($this->query("select * from $table where `id` = ?", [$id], static::class)); } public function findOneBy($params, $values = []) { $table = $this->getTableName(); return current($this->query("select * from $table where $params limit 1", $values, static::class)); } public function findAll() { $table = $this->getTableName(); return $this->query("select * from $table", [], static::class); } public function findAllBy($params, $values = []) { $table = $this->getTableName(); return $this->query("select * from $table where $params", $values, static::class); } public function save() { if ($array = array_diff_assoc($this->getProperties(), $this->originalProperties)) { $table = $this->getTableName(); $params = []; $values = []; foreach ($array as $param => $value) { $params[] = "`$param` = ?"; $values[] = $value; $this->originalProperties[$param] = $value; } $params = implode(', ', $params); if ($this->id) { $values[] = $this->id; $this->query("update $table set $params where `id` = ?", $values); } else { $this->query("insert into $table set $params", $values); $this->id = $this->originalProperties['id'] = self::$PDO->lastInsertId(); } } } public function delete() { $table = $this->getTableName(); $this->query("delete from $table where `id` = ?", [$this->id]); $this->id = $this->originalProperties['id'] = null; } }