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

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

问题描述

我正在学习高级PHP标准,并尝试实现新的有用方法.早先我使用__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_autoload spl_autoload_register

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

推荐答案

spl_autoload_register()允许您注册PHP将放入栈/队列中的多个函数(或您自己的Autoload类中的静态方法),并在以下情况下顺序调用声明新类".

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(),这使您可以选择变量并将其用于包括"相应的类/文件.因此,您不需要特别通过include/require语句来包含该类...

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 ,该手册指出:


With regards to spl_autoload, the manual states:

此功能旨在用作__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.

希望这对您有帮助...

Hope this helps...

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

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