PHP 多个 __autoload 函数*不*使用 spl_register_autoload?

发布于 2024-08-25 19:36:34 字数 386 浏览 2 评论 0原文

我是一个不断增长的 PHP + QuickBooks 相关代码库的作者。我想利用 PHP 的 __autoload() 函数,但是我的代码是一个库,其他人可以将其 include() 到他们自己的应用程序中,所以我不能依赖 __autoload() 尚未实现定义的。

有没有办法拥有多个 __autoload() 函数?

我在 PHP 手册中看到了 spl_autoload_register(),但是并不是我的所有用户都安装了 SPL 扩展,所以我不能依赖它。如果有一种方法可以回退到使用它并默认使用正常的 require/include 语句,我可能会考虑这样做。

还有其他人可以解决这个问题吗?只能有一个 __autoload() 函数似乎是一个明显的疏忽......

I'm an author of a growing library of PHP + QuickBooks related code. I'd like to utilize PHPs __autoload() function, however my code is a library that other people can include() into their own applications, so I can't rely on __autoload() being not already defined.

Is there a way to have multiple __autoload() functions?

I saw spl_autoload_register() in the PHP manual, but not all of my users have the SPL extension installed, so I can't rely on that. If there was a way to fall-back to using this and use normal require/include statements by default, I might consider that.

Does anyone else have any other clever solutions to this issue? It seems like a glaring oversight to only be able to have a single __autoload() function...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2024-09-01 19:36:34

我在 PHP 手册中看到了 spl_autoload_register(),但并非所有用户都安装了 SPL 扩展,因此我不能依赖它。

忽略它们,它是默认编译的,甚至无法在 5.3.0 中禁用。不要为了取悦少数人而牺牲你的框架。

I saw spl_autoload_register() in the PHP manual, but not all of my users have the SPL extension installed, so I can't rely on that.

Ignore them, it's compiled in by default and can't even be disabled in 5.3.0. Don't sacrifice your framework to please the minority.

热血少△年 2024-09-01 19:36:34

我决定解决这个问题并保持向后兼容性的方法是编写我自己的“Loader”类。

我现在使用 Loader::load('TheFile.php');,而不是使用 require_once 'TheFile.php'。 Loader::load() 方法执行以下操作:

if ( the function spl_autoload_register exists )
   register an autoloader
   return true
else 
   check if the file has already been included (static array var with a list of files)
   if not included yet
      require $file
   return true

如果 PHP 安装支持自动加载器,这使我可以灵活地使用自动加载器,否则只能执行正常的 require $file 操作;类型的东西。

I have decided that the way to go about this and also preserve backward compatibility is to write my own "Loader" class.

Instead of using require_once 'TheFile.php', I now use Loader::load('TheFile.php');. The Loader::load() method does the following:

if ( the function spl_autoload_register exists )
   register an autoloader
   return true
else 
   check if the file has already been included (static array var with a list of files)
   if not included yet
      require $file
   return true

This gives me the flexibility to use the autoloader if their PHP installation supports it, and otherwise fall back to just doing normal require $file; type stuff.

野生奥特曼 2024-09-01 19:36:34

您可以编写一个 __autoload 函数,它简单地循环执行实际的类加载的回调数组,直到找到该类或直到列表耗尽。

function loader1 ($className)
{
    if (is_file ('foo/' . $clasName))
    {
        include ('foo/' . $className);
        return (true);
    }
}

function loader2 ($className)
{
    if (is_file ('bar/' . $clasName))
    {
        include ('bar/' . $className);
        return (true);
    }
}

function loader3 ($className)
{
    if (is_file ('baz/' . $clasName))
    {
        include ('baz/' . $className);
        return (true);
    }
}

function __autoload ($className)
{
    $autoloaders = array (
        'loader1', 
        'loader2', 
        'loader3'
    );
    foreach ($autoloaders as $loader)
    {
        if ($loader ($className))
        return (true);
    }
    throw new Exception ('Failed to find class ' . $className);
}

注意:这是“即兴的”,我还没有测试过。

You could write an __autoload function that simply loops over an array of callbacks that do the actual class loading until the class has been found or until the list has been exhausted.

function loader1 ($className)
{
    if (is_file ('foo/' . $clasName))
    {
        include ('foo/' . $className);
        return (true);
    }
}

function loader2 ($className)
{
    if (is_file ('bar/' . $clasName))
    {
        include ('bar/' . $className);
        return (true);
    }
}

function loader3 ($className)
{
    if (is_file ('baz/' . $clasName))
    {
        include ('baz/' . $className);
        return (true);
    }
}

function __autoload ($className)
{
    $autoloaders = array (
        'loader1', 
        'loader2', 
        'loader3'
    );
    foreach ($autoloaders as $loader)
    {
        if ($loader ($className))
        return (true);
    }
    throw new Exception ('Failed to find class ' . $className);
}

NOTE: This is "off the cuff", I've not tested it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文