【问题标题】:How to call __autoload() in php code如何在 php 代码中调用 __autoload()
【发布时间】:2012-10-03 05:46:19
【问题描述】:

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

我也见过this thread,但我的应用程序中没有这样的自动加载器类。是否每个应用程序都需要一个单独的类才能使用自动加载?如果没有,我可以拥有像上面这样的单一功能并完成它的任务吗? 谁能解释如何在我的 php 代码中调用上面的 __autoload 函数?

【问题讨论】:

标签: php autoload autoloader


【解决方案1】:

你不要自己调用__autoload(),PHP 在尝试查找类的实现时会这样做。

例如...

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

$customClass = new CustomClass;

...这将调用__autoload(),将CustomClass 作为参数传递。这个(例如愚蠢的)__autoload() 实现将尝试通过在末尾添加 php 扩展名来包含文件。

顺便说一句,您应该改用spl_autoload_register()。然后,您可以拥有多个实现,这在将多个库与自动加载器一起使用时很有用。

...不鼓励使用__autoload(),将来可能会弃用或删除。

Source.

【讨论】:

  • __autoload() 自 PHP 7.2.0 起已弃用。 source
  • @RaghavendraN 是的,spl_autoload_register() 更好。见最后一段。
【解决方案2】:

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

你可以按照下面的例子来实现它:

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

$CustomClassObj = new CustomClass();
$CustomClassObj1 = new CustomClass1();
$CustomClassObj2 = new CustomClass2();

创建新对象时会自动包含所有类文件。

附:为此,您必须提供与类名相同的类文件名。

【讨论】:

    【解决方案3】:

    你们需要使用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!");
       }
    }
    

    【讨论】:

    • 出于好奇,为什么你得到类名的小写版本,然后不使用呢?您是否打算测试两条路径?
    • 因为它会阻止您创建 cLaSS_nAme.php 文件 :)
    • 实际上,如果你说$path = "${class_name}.php"; 我会同意。我问,因为你使用 "${class}.php" 代替,它允许奇怪的大小写文件名。
    【解决方案4】:

    你也可以使用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; 
    
    
    ?>
    

    【讨论】:

      【解决方案5】:

      不要使用 __autoload() 而是使用 spl_autoload_register。 See php manual 上面写着

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

      【讨论】:

      • 我认为“灵活”这个词的意思是您可以将自己实现的功能用作自动加载器。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 2022-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      相关资源
      最近更新 更多