php - 自动加载器实现,预加载所有 类

php - Autoloader implementation, preload all the classes

我是 php 的新手,继承了一个有数百页的网站项目,所有页面都是程序化的(当我对文件进行文本搜索时,甚至没有任何函数定义)。我来自 c# 和 Java 世界。我正在寻找一种增量添加 OOP 的方法。 (他们要我更新前端,我试图说服他们同时修复后端,他们不想使用框架(该死的))。

在研究自动加载器时...好吧,这是我的理解。这是一种注册存储 classes 的文件夹的方法,当您实例化 class、特征等时,它会根据 class/filename/namespace 搜索文件夹并加载适当的定义。

我有几个问题:

自动加载器是否会搜索文件夹并在每个页面生命周期中加载适当的定义(或缓存它们)?

预加载: 有没有办法使用自动加载器或其他替代方法将所有 class 定义预加载到内存中并使它们在所有会话中可用?

如果是这样,在更新 class 文件时,当我对 class 文件进行更改时,我如何告诉此机制将所有内容重新加载到内存?

问题更新:

谢谢你们的回答,这有点帮助,但是...我确实有在 Whosebug 上提出错误问题的坏习惯。

我想避免的事情是通过添加 classes 来减慢页面速度。因此,假设我添加了一个库并使用自动加载器注册了路径。一个页面实例化了一个具有多个依赖项的 class。假设依赖关系图包含 15 个文件。对于每个请求生命周期,服务器加载该页面和该页面上的 15 个其他文件。

由于我来自编译语言,所以不将这些 classes 加载到内存中感觉有点奇怪。所有 classes 加在一起不应超过 5MB。

(或者也许我应该只创建一个 RAM 磁盘并在启动时将所有文件复制到其中,只需要一个符号链接?)

我想你可能误解了自动加载的目的。它只是说明当您的代码调用 PHP 无法识别的 class 时该怎么做。而已。自动加载器只是调用 requires /path/to/classfile,这样 PHP 就会看到 class。

Does autoloader search the folder and load the appropriate definitions on every page lifecycle (or does it cache them)?

没有跨请求的缓存,因此如果您对文件进行更改,下一个 http 请求将包含这些更改。这就像您更改脚本中的任何其他指令一样,例如将 echo 1 更改为 echo 2

Pre-loading: Is there a way to use autoloader, or some alternative, to pre-load ALL class definitions into memory and make them available across all sessions?

不需要这个。一个写得很好的自动加载器有关于在哪里可以找到任何 class 的说明,所以提前加载所有可能的 classes 是一种浪费。如果您仍然 运行 陷入 undefined classes 错误,您需要改进自动加载器或根据当前的自动加载器说明放置 class 文件。

如果您真的想预加载所有 classes,请使用 php.ini 中的 auto_prepend_file 设置。 The docs say

Specifies the name of a file that is automatically parsed before the main file

设置为初始化脚本。在那个脚本中有类似的东西:

//put all your class files in this folder
$dir = '/path/to/classes/folder';
$handle = opendir($dir);

//require all PHP files from classes folder
while (false !== ($item = readdir($handle))){
    $path = $dir.'/'.$item;
    if(is_file($path) && pathinfo($path,PATHINFO_EXTENSION)==='php')
        require_once $path;
}

这是简化的。将任何目录中的所有文件都包含到脚本中存在重大风险,因此我不会这样做。如果你想在子目录中包含文件,你也需要调整它。

基本上不要这样做。只要有一个好的自动加载器。

PHP 中的自动装载机是懒惰的。当 PHP 遇到它不知道的 class 的使用时,它会要求注册的自动加载器(或自动加载器链)去寻找它。自动加载器的工作是找出从何处获取定义了 class 的文件并将其包含在内。在命名 classes 和组织 class 文件时采用某种约定是拥有有用的自动加载器的关键,PHP 社区中出现了一些约定,例如 PSR-4.

Does autoloader search the folder and load the appropriate definitions on every page lifecycle (or does it cache them)?

每个请求都会调用自动加载器,但只有在需要自动加载 class 时才会调用。

Pre-loading: Is there a way to use autoloader, or some alternative, to pre-load ALL class definitions into memory and make them available across all sessions?

我不这么认为,但是随着 classes 的数量增加,这变得越来越浪费。

欢迎来到美妙的[需要引用] 遗产世界 PHP,我强烈建议您查看 Modernizing Legacy Applications In PHP。这就像一本从魔多回到夏尔的攻略指南。

没有人发布我正在寻找的东西,但似乎最好的方法是预构建到 php 5.5 及更高版本中的 OptCache(我的客户使用的是 5.3,所以我不知道)。

https://github.com/zendtech/ZendOptimizerPlus

The Zend OPcache

The Zend OPcache provides faster PHP execution through opcode caching and optimization. It improves PHP performance by storing precompiled script bytecode in the shared memory. This eliminates the stages of reading code from the disk and compiling it on future access. In addition, it applies a few bytecode optimization patterns that make code execution faster.