什么是自动加载;你如何使用 spl_autoload、__autoload 和 spl_autoload_register? [英] What is Autoloading; How do you use spl_autoload, __autoload and spl_autoload_register?

查看:19
本文介绍了什么是自动加载;你如何使用 spl_autoload、__autoload 和 spl_autoload_register?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习高级 PHP 标准并尝试实施新的有用方法.早些时候我使用 __autoload 只是为了避免在每个页面上包含多个文件,但最近我看到了一个关于 __autoload 手册

I am learning advanced PHP standards and trying to implement new and useful methods. Earlier I was using __autoload just to escape including multiple files on each page, but recently I have seen a tip on __autoload manual

spl_autoload_register() 为自动加载类.因此,使用 __autoload() 是不鼓励使用,将来可能会被弃用或删除.

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_autoloadspl_autoload_register

but I really can't figure out how to implement spl_autoload and spl_autoload_register

推荐答案

spl_autoload_register() 允许您注册 PHP 将放入堆栈的多个函数(或来自您自己的 Autoload 类的静态方法)/queue 并在声明新类"时按顺序调用.

spl_autoload_register() allows you to register multiple functions (or static methods from your own Autoload class) that PHP will put into a stack/queue and call sequentially when a "new Class" is declared.

例如:

spl_autoload_register('myAutoloader');

function myAutoloader($className)
{
    $path = '/path/to/class/';

    include $path.$className.'.php';
}

//-------------------------------------

$myClass = new MyClass();

在上面的例子中,MyClass"是你试图实例化的类的名称,PHP 将此名称作为字符串传递给 spl_autoload_register(),它允许你选择变量并使用它来包含"适当的类/文件.因此,您不需要特别需要通过包含/要求语句包含该类...

In the example above, "MyClass" is the name of the class that you are trying to instantiate, PHP passes this name as a string to spl_autoload_register(), which allows you to pick up the variable and use it to "include" the appropriate class/file. As a result you don't specifically need to include that class via an include/require statement...

只需像上面的例子一样简单地调用你想要实例化的类,因为你注册了一个你自己的函数(通过 spl_autoload_register()),它会找出你所有的类所在的位置, PHP 将使用该函数.

Just simply call the class you want to instantiate like in the example above, and since you registered a function (via spl_autoload_register()) of your own that will figure out where all your class are located, PHP will use that function.

使用spl_autoload_register() 的好处是,与__autoload() 不同的是,您不需要在创建的每个文件中都实现自动加载功能.spl_autoload_register() 还允许您注册多个自动加载函数以加快自动加载并使其更容易.

The benefit of using spl_autoload_register() is that unlike __autoload() you don't need to implement an autoload function in every file that you create. spl_autoload_register() also allows you to register multiple autoload functions to speed up autoloading and make it even easier.

示例:

spl_autoload_register('MyAutoloader::ClassLoader');
spl_autoload_register('MyAutoloader::LibraryLoader');
spl_autoload_register('MyAutoloader::HelperLoader');
spl_autoload_register('MyAutoloader::DatabaseLoader');

class MyAutoloader
{
    public static function ClassLoader($className)
    {
         //your loading logic here
    }


    public static function LibraryLoader($className)
    {
         //your loading logic here
    }

<小时>

关于spl_autoload,手册指出:

此函数旨在用作__autoload() 的默认实现.如果没有指定任何其他内容并且 spl_autoload_register() 被调用而没有任何参数,则此函数将用于以后对 __autoload() 的任何调用.

This function is intended to be used as a default implementation for __autoload(). If nothing else is specified and spl_autoload_register() is called without any parameters then this functions will be used for any later call to __autoload().

在更实际的情况下,如果您的所有文件都位于一个目录中,并且您的应用程序不仅使用 .php 文件,还使用具有 .inc 扩展名的自定义配置文件,那么您可以使用的一种策略是添加您的包含 PHP 包含路径的所有文件的目录(通过 set_include_path()).
由于您还需要配置文件,您可以使用 spl_autoload_extensions() 列出您希望 PHP 查找的扩展.

In more practical terms, if all your files are located in a single directory and your application uses not only .php files, but custom configuration files with .inc extensions for example, then one strategy you could use would be to add your directory containing all files to PHP's include path (via set_include_path()).
And since you require your configuration files as well, you would use spl_autoload_extensions() to list the extensions that you want PHP to look for.

示例:

set_include_path(get_include_path().PATH_SEPARATOR.'path/to/my/directory/');
spl_autoload_extensions('.php, .inc');
spl_autoload_register();

由于 spl_autoload 是 __autoload() 魔法方法的默认实现,当您尝试实例化一个新类时,PHP 将调用 spl_autoload.

Since spl_autoload is the default implementation of the __autoload() magic method, PHP will call spl_autoload when you try and instantiate a new class.

希望这有帮助...

这篇关于什么是自动加载;你如何使用 spl_autoload、__autoload 和 spl_autoload_register?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆