Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php代码中调用uu autoload()_Php_Autoload_Autoloader - Fatal编程技术网

如何在php代码中调用uu autoload()

如何在php代码中调用uu autoload(),php,autoload,autoloader,Php,Autoload,Autoloader,我是php新手,我想在代码中使用php5的自动加载功能。我在index.php中编写了下面的代码,但我不明白应该如何以及何时调用_autoload函数 function __autoload($class) { if (file_exists($class . '.php')) { include($class . '.php'); } else { throw new Exception('Unable to load class named $

我是php新手,我想在代码中使用php5的自动加载功能。我在index.php中编写了下面的代码,但我不明白应该如何以及何时调用_autoload函数

function __autoload($class) {
    if (file_exists($class . '.php')) {
        include($class . '.php');
    } else {
        throw new Exception('Unable to load class named $class');
    }
}
我也见过,但我的应用程序中没有这样的自动加载器类。是否每个应用程序都需要单独的类才能使用自动加载?如果没有,我可以有一个单一的功能,如上述,并完成它的任务?
有人能解释一下如何在我的php代码中调用上述自动加载函数吗

您自己不需要调用
\uuuu autoload()
,PHP在尝试查找类的实现时会调用

例如

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

$customClass = new CustomClass;
…这将调用
\uu autoload()
,并将
CustomClass
作为参数传递。这个(比如说愚蠢的)
\uuuu autoload()
实现将通过在末尾添加
php
扩展名来尝试包含一个文件

另外,您应该使用
spl\u autoload\u register()
。然后,您可以有多个实现,这在使用带有自动加载程序的多个库时非常有用

…不鼓励使用
\uuu autoload()
,将来可能会被弃用或删除

.

在PHP中,autoload()是一个神奇的方法,意味着当您尝试创建类的对象时,它会自动调用,如果PHP引擎在脚本中找不到该类,它会尝试调用autoload()神奇的方法

您可以按照以下示例实现它:

function __autoload($ClassName)
{
    include($ClassName.".php");
}

$CustomClassObj = new CustomClass();
$CustomClassObj1 = new CustomClass1();
$CustomClassObj2 = new CustomClass2();
创建新对象时,它将自动包含所有类文件


另外,要使其工作,您必须给类文件名与类名相同。

你们需要使用
require\u一次
,而不是
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!");
   }
}
不要使用uu autoload(),而是使用spl_autoload_寄存器。上面说

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';
    });

    ?>

您还可以使用
spl\u autoload\u register()
加载php文件,这比
自动加载
例如:

<?php 
spl_autoload_register('myautoloadClass::myautoloadMethod');

class myautoloadClass{

   public static function myautoloadMethod(){
   require_once 'yourPath/'.'yourphpFile'.'.php';
   }

}

$obj = new xp; 


?>


出于好奇,为什么您得到了类名的小写版本,却不使用它?你打算测试这两个路径吗?因为它阻止你创建
cLaSS\u nAme.php
file:),实际上,如果你说
$path=“${cLaSS\u nAme}.php”我同意。我问过,因为您使用的是
“${class}.php”
,它允许使用奇怪的大小写文件名。
\uuuu autoload()
从PHP7.2.0开始就被弃用了@RaghavendraN是的,
spl\u autoload\u register()
更好。请看最后一段。我认为“灵活”一词意味着您可以使用自己实现的功能作为自动加载器。