За последние 24 часа нас посетили 20264 программиста и 1006 роботов. Сейчас ищут 395 программистов ...

Получение имени потомка в родителе?

Тема в разделе "PHP для новичков", создана пользователем Вероломство, 3 авг 2022.

  1. Вероломство

    Вероломство Активный пользователь

    С нами с:
    19 июн 2017
    Сообщения:
    615
    Симпатии:
    24
    PHP:
    1. abstract class A
    2. {
    3.     public function __construct()
    4.     {
    5.         var_dump(static::class, get_called_class(), get_class($this)); // B, B, B
    6.     }
    7. }
    8.  
    9. class B extends A
    10. {
    11.  
    12. }
    13.  
    14. new B();
    А что сейчас в тренде так сказать, что использовать?
     
  2. mkramer

    mkramer Суперстар
    Команда форума Модератор

    С нами с:
    20 июн 2012
    Сообщения:
    8.553
    Симпатии:
    1.754
    Лучше ничего. Родителю должно быть пофиг на потомков.
    --- Добавлено ---
    В тренде минимальное использование наследования
     
    artoodetoo и don.bidon нравится это.
  3. Вероломство

    Вероломство Активный пользователь

    С нами с:
    19 июн 2017
    Сообщения:
    615
    Симпатии:
    24
    есть необходимость использовать это
    PHP:
    1. <?php
    2.  
    3.  
    4. namespace core;
    5.  
    6.  
    7. use PDO;
    8.  
    9. abstract class ActiveRecord
    10. {
    11.     private static $PDO;
    12.     public $id;
    13.     public $originalProperties = [];
    14.  
    15.     function __construct(PDO $PDO)
    16.     {
    17.         self::$PDO = $PDO;
    18.  
    19.         $this->originalProperties = $this->getProperties();
    20.     }
    21.  
    22.     private function getProperties()
    23.     {
    24.         return array_filter(get_object_vars($this), function ($property) {
    25.             return !is_array($property);
    26.         });
    27.     }
    28.  
    29.     public function count()
    30.     {
    31.         $table = $this->getTableName();
    32.  
    33.         return $this->query("select count(*) from $table")->fetchColumn();
    34.     }
    35.  
    36.     abstract protected function getTableName();
    37.  
    38.     public function query($sql, $params = [], $class = '')
    39.     {
    40.         $sth = self::$PDO->prepare($sql);
    41.         $sth->execute($params);
    42.  
    43.         return $class ? $sth->fetchAll(PDO::FETCH_CLASS, $class, [self::$PDO]) : $sth;
    44.     }
    45.  
    46.     public function countBy($params, $values = [])
    47.     {
    48.         $table = $this->getTableName();
    49.  
    50.         return $this->query("select count(*) from $table where $params", $values)->fetchColumn();
    51.     }
    52.  
    53.     public function findOne($id)
    54.     {
    55.         $table = $this->getTableName();
    56.  
    57.         return current($this->query("select * from $table where `id` = ?", [$id], static::class));
    58.     }
    59.  
    60.     public function findOneBy($params, $values = [])
    61.     {
    62.         $table = $this->getTableName();
    63.  
    64.         return current($this->query("select * from $table where $params limit 1", $values, static::class));
    65.     }
    66.  
    67.     public function findAll()
    68.     {
    69.         $table = $this->getTableName();
    70.  
    71.         return $this->query("select * from $table", [], static::class);
    72.     }
    73.  
    74.     public function findAllBy($params, $values = [])
    75.     {
    76.         $table = $this->getTableName();
    77.  
    78.         return $this->query("select * from $table where $params", $values, static::class);
    79.     }
    80.  
    81.     public function save()
    82.     {
    83.         if ($array = array_diff_assoc($this->getProperties(), $this->originalProperties)) {
    84.             $table = $this->getTableName();
    85.             $params = [];
    86.             $values = [];
    87.  
    88.             foreach ($array as $param => $value) {
    89.                 $params[] = "`$param` = ?";
    90.                 $values[] = $value;
    91.                 $this->originalProperties[$param] = $value;
    92.             }
    93.  
    94.             $params = implode(', ', $params);
    95.  
    96.             if ($this->id) {
    97.                 $values[] = $this->id;
    98.  
    99.                 $this->query("update $table set $params where `id` = ?", $values);
    100.             } else {
    101.                 $this->query("insert into $table set $params", $values);
    102.  
    103.                 $this->id = $this->originalProperties['id'] = self::$PDO->lastInsertId();
    104.             }
    105.         }
    106.     }
    107.  
    108.     public function delete()
    109.     {
    110.         $table = $this->getTableName();
    111.  
    112.         $this->query("delete from $table where `id` = ?", [$this->id]);
    113.  
    114.         $this->id = $this->originalProperties['id'] = null;
    115.     }
    116. }
     
  4. [vs]

    [vs] Суперстар
    Команда форума Модератор

    С нами с:
    27 сен 2007
    Сообщения:
    10.553
    Симпатии:
    631
    Dependency Injection