php 如何在php代码中调用__autoload()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12702527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call __autoload() in php code
提问by pabz
I am new to php and I want to use php5's __autoload functionality in my code. I wrote below code in my index.php but I don't understand how and when I should call __autoload function.
我是 php 新手,我想在我的代码中使用 php5 的 __autoload 功能。我在 index.php 中编写了以下代码,但我不明白应该如何以及何时调用 __autoload 函数。
function __autoload($class) {
if (file_exists($class . '.php')) {
include($class . '.php');
} else {
throw new Exception('Unable to load class named $class');
}
}
I have seen this threadalso but I do not have such autoloader class in my application. Do every application need a separate class in order to use autoloading? If not can I have a single function like above and complete it's task? Can anyone explain how to call above __autoload function inside my php code?
我也看过这个线程,但我的应用程序中没有这样的自动加载器类。是否每个应用程序都需要一个单独的类才能使用自动加载?如果没有,我可以拥有像上面那样的单一功能并完成它的任务吗?谁能解释一下如何在我的 php 代码中调用上面的 __autoload 函数?
回答by alex
You don't call __autoload()yourself, PHP does when it is trying to find the implementation of a class.
您不会调用__autoload()自己,PHP 在尝试查找类的实现时会调用。
For example...
例如...
function __autoload($className) {
include $className . '.php';
}
$customClass = new CustomClass;
...this will call __autoload(), passing CustomClassas an argument. This (stupid for example's sake) __autoload()implementation will then attempt to include a file by tacking the phpextension on the end.
...这将调用__autoload(),CustomClass作为参数传递。这个(例如愚蠢的)__autoload()实现将尝试通过php在末尾添加扩展名来包含文件。
As an aside, you should use spl_autoload_register()instead. You can then have multiple implementations, useful when using multiple libraries with auto loaders.
顺便说一句,您应该spl_autoload_register()改用。然后,您可以有多个实现,这在将多个库与自动加载器一起使用时非常有用。
...using
__autoload()is discouraged and may be deprecated or removed in the future.
...
__autoload()不鼓励使用,将来可能会被弃用或删除。
来源。
回答by Mohit Mehta
In PHP __autoload() is a magic method, means it calls automatically when you try create an object of the class and if the PHP engine doesn't find the class in the script it'll try to call __autoload() magic method.
在 PHP 中 __autoload() 是一个魔术方法,这意味着当您尝试创建类的对象时它会自动调用,如果 PHP 引擎在脚本中找不到该类,它将尝试调用 __autoload() 魔术方法。
You can implement it as given below example:
您可以按照以下示例实现它:
function __autoload($ClassName)
{
include($ClassName.".php");
}
$CustomClassObj = new CustomClass();
$CustomClassObj1 = new CustomClass1();
$CustomClassObj2 = new CustomClass2();
It'll automatically include all the class files when creating new object.
创建新对象时,它会自动包含所有类文件。
P.S. For this to work you'll have to give class file name same as the class name.
PS 为此,您必须提供与类名相同的类文件名。
回答by Hassan Azimi
You guys need to use require_onceinstead of includewhich will eat your memory
The best way is:
你们需要使用require_once而不是include哪个会吃掉你的记忆最好的方法是:
function __autoload($class) {
$class_name = strtolower($class);
$path = "{$class}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The file {$class}.php could not be found!");
}
}
回答by PHMJ
also you can use spl_autoload_register()for load php file and this is much better than
__autoloadfor example :
您也可以spl_autoload_register()用于加载 php 文件,这比例
__autoload如:
<?php
spl_autoload_register('myautoloadClass::myautoloadMethod');
class myautoloadClass{
public static function myautoloadMethod(){
require_once 'yourPath/'.'yourphpFile'.'.php';
}
}
$obj = new xp;
?>
回答by RN Kushwaha
Do not use __autoload() instead use spl_autoload_register. See php manualwhich says
不要使用 __autoload() 而是使用 spl_autoload_register。请参阅 php 手册,其中说
spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.
spl_autoload_register() 为自动加载类提供了更灵活的替代方法。出于这个原因,不鼓励使用 __autoload() 并且将来可能会被弃用或删除。
<?php
// function __autoload($class) {
// include 'classes/' . $class . '.class.php';
// }
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
// Or, using an anonymous function as of PHP 5.3.0
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});
?>

