php 什么是php中的自动加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3607543/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:24:16  来源:igfitidea点击:

What is autoload in php?

phpautoload

提问by DEVOPS

what is autoload in PHP?

PHP 中的自动加载是什么?

采纳答案by osm

This will be of help to you about usage of autoload. http://ditio.net/2008/11/13/php-autoload-best-practices/

这将有助于您了解自动加载的使用。http://ditio.net/2008/11/13/php-autoload-best-practices/

It's a magic function that helps you include / require files using class name.

这是一个神奇的功能,可以帮助您使用类名包含/需要文件。

function __autoload($class_name) 
{
    require_once $DOCUMENT_ROOT . “/classes/” . $class_name .“.php”;
}

It's deprecated at PHP 7.2.0 and spl_autoload_register is recommended for that purpose.

它在 PHP 7.2.0 中已被弃用,为此推荐使用 spl_autoload_register。

回答by Muhammad Shahzad

What is autoloading?

什么是自动加载?

Every time you want to use a new class in your PHP project, first you need to include this class (using include or require language construct, that's right this are not functions). However if you have __autoload function defined, inclusion will handle itself.

每次你想在你的 PHP 项目中使用一个新类时,首先你需要包含这个类(使用 include 或 require 语言构造,没错,这不是函数)。但是,如果您定义了 __autoload 函数,则包含将自行处理。

include "classes/class.Foo.php";

$foo = new Foo;
$foo->start();
$foo->stop();

Basic Autoloading Example

基本自动加载示例

function __autoload($class_name) 
{
    require_once $DOCUMENT_ROOT.“classes/class.”.$class_name.“.php”;
}

$foo = new Foo;
$foo->start();
$foo->stop();

PHP Official

PHP官方

Other

其他

Update

更新

PHP 5 introduced the magic function __autoload() which is automatically called when your code references a class or interface that hasn't been loaded yet.

PHP 5 引入了魔术函数 __autoload() ,当您的代码引用尚未加载的类或接口时会自动调用该函数。

The major drawback to the __autoload()function is that you can only provide one autoloader with it. PHP 5.1.2 introduced spl_autoload()which allows you to register multiple autoloader functions, and in the future the __autoload()function will be deprecated.

__autoload()功能的主要缺点是您只能提供一个自动加载器。PHP 5.1.2 引入了spl_autoload()它允许您注册多个自动加载器函数,将来该__autoload()函数将被弃用。

The introduction of spl_autoload_register()gave programmers the ability to create an autoload chain, a series of functions that can be called to try and load a class or interface. For example:

引入spl_autoload_register()使程序员能够创建自动加载链,这是一系列可以调用以尝试加载类或接口的函数。例如:

<?php
function autoloadModel($className) {
    $filename = "models/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

function autoloadController($className) {
    $filename = "controllers/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

spl_autoload_register("autoloadModel");
spl_autoload_register("autoloadController");

回答by slikts

Here is the official documentation: http://php.net/autoload

这是官方文档:http: //php.net/autoload

In short, it just allows you to define search paths for classes so you wouldn't be required to include the files containing them manually.

简而言之,它只允许您定义类的搜索路径,因此您不需要手动包含包含它们的文件。

I suggest you should develop a habit of searching php.net by just appending function names or obvious keywords to the address. That's how I found php.net/autoload. It's quite convenient like that.

我建议你应该养成搜索 php.net 的习惯,只需在地址后附加函数名称或明显的关键字。这就是我找到 php.net/autoload 的方式。这样倒是挺方便的。

回答by kumardippu

an __autoload() 

//function which is automatically called in case you are trying to use     
//a class/interface which hasn't been defined yet.

function __autoload($class_name) {
    include $class_name . '.php';
}