简体   繁体   English

Zend中的spl_autoload_register

[英]spl_autoload_register in Zend

I'm currently trying to use the Twilio PHP library that uses spl_autoload_register to include its classes. 我目前正在尝试使用使用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');

I throw in this code: 我抛出这段代码:

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

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

And then I get this error when running it: 然后我在运行时遇到此错误:

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

This code runs off Zend, and already has a bootstrap with _initAutoload(). 这段代码运行Zend,并且已经有一个带有_initAutoload()的引导程序。 I'm not sure where or how I should implement the autoload for this library as I'm not very familiar with it. 我不知道我应该在哪里或如何为这个库实现自动加载,因为我对它不是很熟悉。

I think I have reproduced the problem. 我想我已经复制了这个问题。

To correct it, I just add require_once('Services/Twilio.php'); 要纠正它,我只需添加require_once('Services/Twilio.php'); in the bootstrap like this: 在引导程序中像这样:

require_once('Services/Twilio.php');

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{    
  ....

In my case, I put Services directory in library directory (where is Zend directory). 在我的例子中,我将Services目录放在library目录中(Zend目录在哪里)。

And in a controller, I can call Services_Twilio like you : 在控制器中,我可以像你一样调用Services_Twilio

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

I hope it will help you. 我希望它会对你有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM