美文网首页
PHP autoload

PHP autoload

作者: 黄耀鸿 | 来源:发表于2020-08-14 20:32 被阅读0次

    In general, when we use a file which not include in current file, we need to load it in current file by using `require` or `include`;

    But when a project has a lot of php files, it is not a good way to include each file manually.

    Let's see a simple code:

    ```php

    <?php

    $person = new Person("Rhys", 20);

    var_dump($person);

    ```

    There is a new class `Person` which didn't be load in current file. In this case, php will call default autoload method `spl_autoload()` in order to load possible class;

    In OPCODE file, it call `FETCH_CLASS` method. Refer to the source file, we found that it call `autoload_func`;

    `spl_autoload_call` will try all registered `__autoload()` functions to load the requested class;

    `spl_autoload()`: spl_autoload ( string $class_name [, string $file_extensions = spl_autoload_extensions() ] ) : void

    The `spl_autoload_register()` function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

    REFERENCE:

    https://www.php.net/manual/en/language.oop5.autoload.php

    https://segmentfault.com/a/1190000006188247

    相关文章

      网友评论

          本文标题:PHP autoload

          本文链接:https://www.haomeiwen.com/subject/zdundktx.html