如何允许在 CakePHP 中自动加载自定义库?

发布于 2024-11-30 17:31:12 字数 424 浏览 0 评论 0原文

您好,我正在使用 CakePHP,我需要将一些供应商文件添加到自动加载中。这是正确的方法吗:

在 bootstrap.php 中,我有以下代码:

function __autoload($className){

        $classFile = '../vendors/phprtf/' . str_replace('_', '/', $className) . '.php';

        // check if file exists
         if(file_exists($classFile)) {
            require $classFile;
        }

}

PHPRTFLite 在各个子目录下有很多类文件。因此,列出所有这些并不是一个好的选择。

目前看来是有效的。

Hi I am using CakePHP and there are some Vendor files I need to add to the autoloading. Is this the correct method:

In the bootstrap.php, I have the following code:

function __autoload($className){

        $classFile = '../vendors/phprtf/' . str_replace('_', '/', $className) . '.php';

        // check if file exists
         if(file_exists($classFile)) {
            require $classFile;
        }

}

The PHPRTFLite has a lot of class files under various sub directories. So listing all of them is not a good option.

It seems to be working for now.

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

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

发布评论

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

评论(1

夜灵血窟げ 2024-12-07 17:31:12

您应该使用 spl_autoload_register()因为它提供的灵活性。

如果您的代码有一个现有的__autoload函数,那么这个函数
必须在 __autoload 堆栈上显式注册。这是因为
spl_autoload_register() 将有效地替换引擎缓存
通过 spl_autoload()__autoload 函数
spl_autoload_call()

如果必须有多个自动加载函数,spl_autoload_register()
允许这样做。它有效地创建了一个自动加载函数队列,
并按照定义的顺序遍历它们中的每一个。经过
相反,__autoload() 只能定义一次。

You should use spl_autoload_register() for the flexibility it offers.

If your code has an existing __autoload function then this function
must be explicitly registered on the __autoload stack. This is because
spl_autoload_register() will effectively replace the engine cache for
the __autoload function by either spl_autoload() or
spl_autoload_call().

If there must be multiple autoload functions, spl_autoload_register()
allows for this. It effectively creates a queue of autoload functions,
and runs through each of them in the order they are defined. By
contrast, __autoload() may only be defined once.

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