0%

PHP 加载机制

PHP 加载方式

  • (include/require)_once
  • __autoload
  • spl_autoload_register

手动加载

(include/require)_once

include vs require

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

xxx vs xxx_once

xxx_once 会有性能问题,尽量不用。原因有两个:

自动加载

__autoload

若类在当前空间找不到,则尝试调用 autoload,若autoload已声明

spl_autoload_register

注册多个 autoload 到一个队列中。若类在当前空间找不到,会依次执行队列中的 autoload 方法,这种方式就是支持多个 __autoload