E
E
Evgeny Yerko2016-07-25 01:10:55
PHP
Evgeny Yerko, 2016-07-25 01:10:55

Organize phpDoc for __get property where property is an instance of a class, how?

Hello everyone!
Something does not come out correctly to write phpDoc so that autocomplete would work in phpstorm.
Below is the code itself.
P.S. The code is not mine, I don’t want to rewrite it, since there are many projects on this engine.
Base.php

<?php
/**
 * Class Base
 * @property $config
 * @property $request
 * @property $db
 */
Class Base {

   
  private $classes = array(
    'config'     => 'Config',
    'request'    => 'Request',
    'db'         => 'Database'
  );
  
  private static $objects = array();
  
  public function __get($name)
  {
    // Если такой объект уже существует, возвращаем его
    if(isset(self::$objects[$name]))
    {
      return(self::$objects[$name]);
    }

    // Если запрошенного API не существует - ошибка
    if(!array_key_exists($name, $this->classes))
    {
      return null;
    }

    // Определяем имя нужного класса
    $class = $this->classes[$name];

    // Подключаем его
    include_once(dirname(__FILE__).'/'.$class.'.php');

    // Сохраняем для будущих обращений к нему
    self::$objects[$name] = new $class();

    // Возвращаем созданный объект
    return self::$objects[$name];
  }
}

config.php
<?php

require_once('Base.php');

class Config
{

  private $vars = array();
  public function __construct()
  {
    //....
  }
  //....
}

Database.php
<?php
require_once('Base.php');

class Database extends Base
{
  private $mysqli;
  private $res;

  public function __construct()
  {
    parent::__construct();
    $this->connect();
  }
  
  public function connect() {/*....*/}
  
  public function query() {/*....*/}
  
  public function placehold() {/*....*/}
  
  //....
}

Request.php
<?php
require_once('Base.php');

class Request extends Base
{

  public function __construct()
  {
    parent::__construct();

    //....

  }
  
  public function method($method = null) {/*....*/}
  
  private function stripslashes_recursive($var) {/*....*/}
  //....
  
}

Thank you for your attention, I will be grateful for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2016-07-25
@OsBen

/**
 * Class Base
 * @property Config $config
 * @property Request $request
 * @property Database $db
 */

Or I didn't quite understand what you are asking.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question