I want to ask about the autoload function __autoload in PHP .
Yes, I read that the function will soon be considered obsolete and I know about alternative new methods. But, I can not understand one question, the answer to which I did not find on the Internet. The value of the __autoload function __autoload passed to $className . Question: how and from where is it transmitted?

For example. There are folders, lib , modules , core , etc. The classes folder contains the classes (in the appropriate files) router , controller_base , template , register , module . The __autoload function __autoload described in the core file, in a separate core folder and includes only three classes of router , template , controller_base .
Why??? And How??? I can not understand for what whim is exactly such subalue when there are more than three classes!

    2 answers 2

    There is no need to call a function; it is automatically called at the moment when the interpreter encounters the name of a class that has not yet been loaded. Thus, only those classes that are actually used will be loaded.

    Koter, PHP 7, 2016, p. 501.

    This is a common function if you create an instance of the class $var = new MyClass(); The function __autoload('MyClass') automatically called. To intercept it, you override in your code by creating this function, for example:

     function __autoload($classname) { $filename = "./". $classname .".php"; include_once($filename); } 

    Source http://php.net/manual/ru/function.autoload.php

    In your case, three base classes are apparently connected by type MVC (model, view, controller), and all other classes already connect these base ones as needed. As an example, why do you need to connect the product catalog class on the contact page?

    • You are right that this is mvc but not my but an example from one of the articles. I'm just trying to figure out how everything works line by line, because I'm new to AC. Thanks - privetsh

    Not exactly the answer to the question, but I can not write.

    He himself suffered a lot in his time with the search for the “ideal” class autoloading scheme, reread many articles, revised many examples, but all of them were either not very good (to put it mildly) or sharpened under rather strange conditions.

    Therefore, in order to increase the entropy of the universe, I will tell what I came to. Maybe someone else will save the day.

    DISCLAIMER: This decision does not claim to be the only correct one. Use at your own risk.

    Prerequisites:

    1. Class file names are identical to class names with the suffix '.php'
    2. Classes are declared using namespaces.
    3. Namespaces correspond to subdirectories in which class files lie

    For example, we have the class AppDb in the core\database namespace, then the file with this class will contain the namespace core\database; and located on the path core/database/AppDb.php

    The autoloader function for this structure will be as follows:

     const CLASS_ROOT = '/srv/www/htdocs/class/'; //"корневой" каталог для классов spl_autoload_register( function ($class) { $class = str_replace('\\', '/', $class); $file = CLASS_ROOT . $class . '.php'; if (is_file($file)) { include_once $file; return; } } ); 
    • Thanks and you, too, helped! - privetsh