symfony 的自动加载器是否足够聪明,只能加载必要的文件?

发布于 2024-08-23 05:05:42 字数 139 浏览 4 评论 0原文

我知道 symfony 可以使用 spl_auto_register 自动加载文件,但是它足够聪明吗?

如果只需要加载a.php,是否也会加载b.php

I know symfony can autoload files with spl_auto_register,but is it clever enough?

If only a.php is needed to load,will it also load b.php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北凤男飞 2024-08-30 05:05:42

自动加载器通过存储键值对数组来工作,以便

classname => /full/path/to/class

在 symfony 1.3/1.4 中创建两个缓存文件。一个用于核心类,由 sfCoreAutoload 创建,并存储在 /cache/project.autoload 中。对于从项目目录中自动加载的类,键值对数组存储在/cache/_app_/_env_/config/config_autoload.yml.php中。该文件由配置处理程序 sfAutoloadConfigHandler 生成。

需要记住的一些有用的要点:

  • 并非项目中的所有类都会自动加载,自动加载的文件由 autoload.yml 指定(在 sf_lib_dir/config/config 中)。您可以创建自己的 autoload.yml 并将其放入项目配置目录(或者我认为甚至是应用程序配置目录)。
  • 当在开发环境中运行时,如果一个类无法自动加载,那么 symfony 将重建自动加载缓存,希望找到它假设是新的文件。这可能会使您的应用程序在某些用例中变慢,因此请使用 sfAutoloadAgain::unregister()。

The autoloader works by storing an array of key value pairs such that

classname => /full/path/to/class

In symfony 1.3/1.4 two cache files get created. One for the core classes, created by sfCoreAutoload, and stored in /cache/project.autoload. For the classes that are autoloaded from within your project directory, the array of key value pairs is stored in /cache/_app_/_env_/config/config_autoload.yml.php. This file is generated by the config handler sfAutoloadConfigHandler.

Some useful points to remember:

  • Not all classes within your project are autoloaded, the autoloaded files are specified by autoload.yml (in sf_lib_dir/config/config). You can create your own autoload.yml and drop it into the project config dir (or even application config dir I think).
  • When running in dev environment, if a class cannot be autoloaded then symfony will rebuild the autoload cache hoping to find the file its assuming is new. This can make your app slow in certain use cases so use sfAutoloadAgain::unregister().
奶气 2024-08-30 05:05:42

自动加载器将仅加载实例化所请求的类所需的文件。

The autoloader will only load the files necessary to instantiate a requested class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文