gpt4 book ai didi

php - Zend 中的 spl_autoload_register

转载 作者:搜寻专家 更新时间:2023-10-31 21:34:06 30 4
gpt4 key购买 nike

我目前正在尝试使用使用 spl_autoload_register 来包含其类的 Twilio PHP 库。

function Services_Twilio_autoload($className) {
if (substr($className, 0, 15) != 'Services_Twilio') {
return false;
}
$file = str_replace('_', '/', $className);
$file = str_replace('Services/', '', $file);
return include dirname(__FILE__) . "/Twilio.php";
}

spl_autoload_register('Services_Twilio_autoload');

我输入这段代码:

require_once('Library/Services/Twilio.php');

$client = new Services_Twilio($this->sid, $this->token);

然后我在运行时遇到这个错误:

Fatal error: Cannot redeclare Services_Twilio_autoload() (previously declared in ...\Twilio\Library\Services\Twilio.php:9) in ... \Twilio\Library\Services\Twilio.php on line 16

此代码在 Zend 上运行,并且已经有一个带有 _initAutoload() 的 Bootstrap 。我不确定应该在哪里或如何为这个库实现自动加载,因为我对此不是很熟悉。

最佳答案

我想我已经重现了这个问题。

要更正它,我只需在 Bootstrap 中添加 require_once('Services/Twilio.php');,如下所示:

require_once('Services/Twilio.php');

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
....

在我的例子中,我将 Services 目录放在 library 目录(Zend 目录在哪里)中。

在 Controller 中,我可以像您一样调用 Services_Twilio:

$client = new Services_Twilio($this->sid, $this->token);

希望对您有所帮助。 :)

关于php - Zend 中的 spl_autoload_register,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038748/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com