Here in the file I connect

include '/map.php'; include '/code.php'; include '/boot.php'; 

How to make the files themselves connected when needed?

In which case _get () fires


Added an example

 class vasek { private $classes = array( 'config' => 'Config', 'request' => 'Request' ); private static $objects = array(); public function __construct(){ } public function __get($name) { if(isset(self::$objects[$name])){ return(self::$objects[$name]); } if(!array_key_exists($name, $this->classes)){ return null; } $class = $this->classes[$name]; include_once('api/'.$class.'.php'); self::$objects[$name] = new $class(); return self::$objects[$name]; } } 

    2 answers 2

    It is not clear what you decided (and everyone believed) that you need a magician. __get method when it comes to attaching files.

    Autoloader needed:

     <?php // или так: function my_autoloader($class) { include 'classes/' . $class . '.php'; } spl_autoload_register('my_autoloader'); // или так: // function __autoload($class) { // include 'classes/' . $class . '.php'; // } ?> 

    After adding this code, the required file from the "classes" directory will be included, provided that it is correctly named. For example, you create an instance of the map class:

     <? $map = new map(); $map->some_method(); ?> 

    So, if there was no class declaration at this point, an attempt will be made to connect the "./classes/map.php" file in the hope that the required class is described in it.

    Read more: PHP OOP5 - autoload


    UPD

    That is, if you want to get an object of the class map when you call $vasek->map map , you need to add your classes to the $classes array. It is strange why this is done in this way, but what is, that is. I, for example, just checked for the presence of the file of the same name. But you need this:

     class vasek { private $classes = array( 'config' => 'Config', 'request' => 'Request', 'map' => 'Map', ); ... } 

    and then - move the file "map.php" to "api / Map.php" (follow the register!).

    Then at the first call to $vasek->map file "api / Map.php" will be connected, an instance of the class will be created: new Map() and saved in the closed $objects array - so that when you next access this message, you’ll return to the already created an object.

    • The autoloader is not used in the Simple cms and the files are loaded __get Moreover, this file will not work on 5.1 - dfhsfhgfj
    • How did you know that we are talking about some kind of cms? php.net/manual/en/function.spl-autoload-register.php spl_autoload_register (PHP 5> = 5.1.2) - Bars
    • Hmm, I thought and understood what was going on. I once wrote my own framework (Denis and I have been writing for over 5 years), and so there was such a thing: the main program object had only the necessary overhead of methods and the ability to extend with “modules” - this happened when trying to access the property : if it is not defined, then in the __get handler, a class object was created (of the same name with the property name) and assigned to the name of this property. It turned out that during the first access to $core->users an object was created and returned, with subsequent $core->users , the object already existed. About this speech? - Bars
    • (PHP 5> = 5.1.2) - this is the whole problem, I have clearly 5.1.0 from above I wrote an example from the simples, but I still didn’t understand how to pervert so that it would work only if inheriting everything from this class - dfhsfhgfj
    • Well, yes, it is, as described in my comment above: when accessing the $vasek->config property, the __get handler looks to see if the $vasek->classes personal property is the class name ("config"), and, if there is , includes the file "./api/Config.php" with the expectation that the class "Config" is described in this file. The solution to your problem, as far as I understood it, was added in response. - Bars

    This magic function should be considered together with the function of the same type - __set ().

    In __set() and __get() the principles of creating and the principles of obtaining an unknown property (not described in a class) for a class are described.

    So, for example, it is possible to record unknown properties up to a certain point in an array and from there to call them when accessing them, after the declaration.

    Example:

     <?php class A { //свойство класса (в будущем - массив) для хранения не предопределенных переменных public $data; //определяем функцию хранения неизвестной классу переменной function __set($property, $value){ $this->data[$property] = $value; } //определяем функцию извлечения добавленной ранее переменной function __get($property){ return $this->data[$property]; } } // тестируем работу магических функций //создаём объект класса $object = new A(); //присваиваем значение новому свойству name $object->name = 'Значение свойства name'; // работает магический метод __set(), записывая значение в массив $data //выводим не предопределенное в классе свойство name echo $object->name; # сработает метод __get(), достаёт из массива date значение ячейки с ключём name, выведет - Значение свойства name ?> 

    Plus, you can read the page in the documentation about this.