__autoload 与 include 系列
我今天发现了 __autoload 函数,在阅读了该函数的官方手册页后,有一点我根本不明白。
使用 __autoload() 和 require_once 之间有什么明显的区别?
因为它看起来自动加载是执行所需包含的新时尚方式但对我来说,使用require_once代替要好得多。因此,必须将 __autoload 定义到所有 php 文件中,这意味着将其代码写在那里,如果我将 ALL 我的includes/require_once/...等放入一个文件中,我们将其称为 main_header .php 那么我需要在我的网络应用程序文件中做的就是编写一行代码:
<?php require_once('main_header.php'); ?>
我在某个地方错了吗?
I've discovered the __autoload function today and after reading the official manual page of this function there's a point I don't get at all.
What's clearly the difference between using __autoload() and let's say require_once?
because it looks autoload is the new fashion way of doing the required includes but to me it's far better to use require_once instead. Hence, __autoload has to be defined into all the php files which means writing its code down there, if I put ALL my includes/require_once/...etc into one file let's call it main_header.php then all I'll need to do in my web app files is write one line of code:
<?php require_once('main_header.php'); ?>
Am I wrong somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我可以看到自动加载的两件事(不一定
__autoload
;更喜欢更现代的spl_autoload_register
):main_header.php
,但下一项将生效。还值得指出的是,当反序列化尚未定义的类的对象时,也会触发自动加载,这使得事情变得更加实用。当然,
unserialize
中还有另一个钩子(配置设置unserialize_callback_func),因此自动加载在技术上不是必需的。但它绝对更好。I can see two things going for autoloading (not necessarily
__autoload
; prefer the more modernspl_autoload_register
instead):main_header.php
as in your example, but then the next item comes into effect.It's also worth to point out that autoloading is also triggered when unserializing an object of a class that has not been yet defined, which makes things infinitely more practical. Of course there is another hook in
unserialize
for this (the configuration settingunserialize_callback_func
), so autoloading is not technically necessary. It's definitely nicer though.第一:使用
spl_autoload_register()
而不是__autoload()
。当您尝试访问不存在的类时,会调用自动加载器。使用
include()
您只需包含一个类。主要区别在于,使用自动加载器仅在确实需要/使用该类时才包含该类。通常,您在一个文件中定义一个自动加载器,并在每个请求中包含该自动加载器。如果您使用引导(意思是:单个文件,捕获每个请求并将其重定向到适当的目标),则只需要在那里定义自动加载器。所以不需要在每个文件中定义它。
First: Use
spl_autoload_register()
instead of__autoload()
.The autoloader is called, when you try to access a class, that doesn't exists. With
include()
you just include a class. The main difference is, that using an autoloader the class is only included, if its really required/used.Usually you define an autoloader in one file and include this one on every request. If you use bootstrapping (meaning: a single file, that catches every request and redirects it to the appropriate target) its only required to define the autoloader there. So its its not required to define it in every file.
自动加载器用于延迟初始化。它对于 MVC 架构来说是最有效的,而不是对于到处包含文件并在每个文件中定义数据库连接字符串的网站(这很糟糕)。
将自动加载与 MVC 框架结合使用可以节省资源并为模块化带来很多好处。由于您不必包含带有类的文件,因此您只需在当前所在的控制器中实例化所需的类即可。
基本上,这是一个 OOP 的事情。如果您不使用对象方法来构建您的网站,并且 include/require 适合您,那么您不必担心。
Autoloader is used for lazy initialization. It's the most effective with MVC architecture, not with websites that include a file here and there and define db connection string in every file (which is terrible).
Using autoload with MVC framework saves resources and brings a lot to modularity. Since you don't have to include files with classes, you just instantiate a class you require in the controller you're currently at.
Basically, it's an OOP thing. You shouldn't worry about it if you're not using object approach to structuring your website and include/require is what will work for you.
我认为这取决于个人喜好。对我来说,自动加载是一种典型的 php 类型的东西 - 一种类似“魔术引号”的肮脏黑客,它的存在只是因为他们懒得坐下来编写一个干净的解决方案来解决问题 - 在这种情况下,一个足够的解决方案打包系统+可捕获的 ClassNotFound 异常。在解决这个问题之前,我将坚持使用
include
。I think it comes down to personal preference. For me, autoload is a typical php kind of thing - a dirty "magic-quotes"-alike hack, that only exists because they don't bother to sit down and code a clean solution to the problem - in this case, an adequate packaging system + a catchable ClassNotFound exception. Until we've got this fixed, I'll stick to
include
.选择适合工作的工具。自动加载用于加载之前使用的未定义类的源代码。因此,您可以实例化到目前为止不需要文件的类。
如果您使用类并且希望将文件放在一个中心位置(单个点)而不是分布在不同的文件中,那么这是有意义的。
但是,如果您使用包含并需要构建一个带有菜单和页脚的网站,那么这是没有意义的。自动加载对此不起作用:
如果您使用需要加载函数,则也无法使用自动加载。
因此,仅因为自动加载在您看来有些其他人喜欢,并不意味着它适合您的需求。
我个人在不想满足类需求并且希望能够从模块化目录和库结构中动态加载类的项目中使用它。
我只使用
spl_autoload_register()
注册一个或多个自动加载实现。这是在 PHP 中注册自动加载函数的推荐方法,因为它允许拥有多个自动加载器。Choose the right tool for the job. Autoloading is for loading the source-code of yet undefined Classes prior use. So you can instantiate classes you have not required the files for so far.
This makes sense if you work with classes and you want to require files at a central place, a single point instead all over in different files.
But if you're using includes and requires to let's say build a website with menu and footer, this does not make sense. Autoload does not work for this:
If you're making use of requires to load functions, you can not use autoload either.
So only because autoload looks somewhat fancied by others to you, it does not mean that it fits your needs.
I personally use it in projects where I do not want to take care of the requires for classes and where I want to be able to dynamically load classes from within a modular directory and library structure.
I only make use of
spl_autoload_register()
to register one or more autoload implementations. It's the recommended method to register an autoload function in PHP as it allows to have multiple autoloaders.所有的答案都非常正确和充分。我只是想我应该加上我的两分钱。
首先, *_once 函数很慢。不要使用它们。
其次,在我正在开发的应用程序中,我有很多相互依赖的类,并且我并不总是知道页面中需要哪些类。
而不是创建一个 main.php 页面并包含所有大量的类;显然浪费资源和时间(包含和要求很昂贵),我使用自动加载来在页面需要这些文件时包含这些文件。
因此,我所做的就是编写代码,并且类会自动添加。
这也改进了您的代码和目录结构,因为您遵循命名类和文件的系统。
只是一个补充。在资源使用和速度方面(需求递减顺序和速度递增顺序):
Require_once() ; include_once();要求() ; include()
使得 include 最好、最快。
All the answers are more than correct and adequate. I just thought I should add my two cents.
First, *_once functions are slow. Do not use them.
Secondly, in an app I am developing, I have lots of classes which I rely on each other and I do not always know which classes are needed in a page.
Instead of creating a main.php page and including all the classes which are a lot; obviously wasting resources and time (Include and require are expensive), I use autoload to include those files as and when they are needed by the page.
All I do therefore is code and the classes are added automatically.
This also improves your code and directory structure since you follow a system in naming your classes and files.
Just an addition. In terms of resource usage and speed(decreasing order of requirements and increasing order of speed):
Require_once() ; include_once() ; require() ; include()
Making include the best and fastest.
自动加载和包含系列功能都有其用武之地。
自动加载旨在延迟加载基于 OO 的脚本中的模块(即类和接口),并在您使用 new 或静态调用尚未加载的类的方法时调用。这会触发自动加载来运行一个尝试加载(通常通过包含)相关类或接口的函数。这种方法的优点是类仅在需要时才加载。任何不被使用的类都不会被加载。
自动加载不能用于加载不是类或接口的程序片段,对于这些项目,您仍然需要包含系列。
至于必须为每个需要自动加载的脚本添加自动加载功能?在这种情况下,您将自动加载函数放在自己的文件中,并将其包含在需要自动加载的脚本中。 :)
Autoload and the include family functions both have their place.
Autoload is intended for lazy loading of modules in an OO based script (namely classes and interfaces) and is invoked when you use new or statically call a method of a class that hasn't yet been loaded. This triggers autoload to run a function that attempts to load (typically via include) the class or interface in question. The advantage of this approach is that classes are only loaded as and when they are needed. Any class that doesn't get used doesn't get loaded.
Autoload cannot be used for loading program fragments that aren't a class or an interface, for those items you still need the include family.
As for having to add an autoload function to every script that needs to autoload? In this case you put the autoload function in its own file and include it in the scripts that need to autoload. :)
另请注意:在单元测试时,include 和 require 可能会成为一个很大的痛苦。
我不知道应该如何防止要求/包括任何东西。有 测试助手,它可以做一些有用的事情,例如防止退出或死亡,但他们仍然无法模拟包含或要求。
Mockery 库 建议 使用自动加载来模拟公共静态方法。
Another note: include and require may become a big pain while unit testing.
I don't know how should I prevent requiring / including anything. There is test helpers, that can do some useful things, such as preventing exiting or dieing, but they still cannot mock include or require.
Mockery library recommends using autoloading to be able to mock public static methods.