PHP autoload

更新中...

Posted by yaohong on Friday, August 14, 2020

TOC

php autoload

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
$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

「点个赞」

Yaohong

点个赞

使用微信扫描二维码完成支付