Answer the question
In order to leave comments, you need to log in
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];
}
}
<?php
require_once('Base.php');
class Config
{
private $vars = array();
public function __construct()
{
//....
}
//....
}
<?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() {/*....*/}
//....
}
<?php
require_once('Base.php');
class Request extends Base
{
public function __construct()
{
parent::__construct();
//....
}
public function method($method = null) {/*....*/}
private function stripslashes_recursive($var) {/*....*/}
//....
}
Answer the question
In order to leave comments, you need to log in
/**
* Class Base
* @property Config $config
* @property Request $request
* @property Database $db
*/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question