我在application/core文件夹中创建了一个名为MY_library的核心库,我正试图从application/libraries中的库类扩展它,但不幸的是,它找不到文件。
//application/core/My_Library.php
class My_Library{
function __construct(){
}
/**
* This is the generic function that calls php curl to make a query.
* @param $url
* @param array $data
* @param string $type
* @return mixed|string
*/
public function callService($url,$data=array(),$type="get"){
if (strtolower($type) == "get"){
$url .= "?".http_build_query($data);
$response = $this->doGet($url);
}else if (strtolower($type) == "post"){
$fields_string = http_build_query($data);
$response = $this->doPost($url,$fields_string);
}else{
$response = "INVALID REQUEST";
}
return $response;
}
}
In my application/libraries
class CakePixel extends MY_Library{
function __construct(){
parent::__construct();
}
public function fireCakePixel($cakeOfferId,$reqId,$transactionId){
$cakeUrl = "http://oamtrk.com/p.ashx";
$param = array(
"o" => $cakeOfferId,
"reqid" =>$reqId,
"t" => $transactionId
);
$response = $this->callService($cakeUrl,$param,"get");
}
}
但我得到了一个致命的错误
PHP Fatal error: Class 'MY_Library' not found in /application/libraries/cakeApi/pixel/CakePixel.php on line 10, referer:
如果可能的话,我如何在不使用require_one或include-from-class文件的情况下解决这个问题。
不应在core
目录中加载库。core
目录用于核心类或您希望控制器从中扩展的"父"控制器。您应该在Codeigniter的libraries
目录中加载所有库,然后,在您的控制器中,您可以调用库中的函数,如下所示:
$this->load->library('my_library');
$results = $this->my_library->callService($params);
CI总是首先查找系统库(如果存在),然后在核心或应用程序下的库文件夹中查找MY_,系统或库目录中没有library.php,这就是为什么会出现此错误。如果您想从核心或库目录自动加载第三方库,您可以使用以下代码将其添加到底部或顶部的config.php中
spl_autoload_register(function($class)
{
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'core/' . $class . '.php'))
{
include $file;
}
elseif (file_exists($file = APPPATH . 'libraries/' . $class . '.php'))
{
include $file;
}
}
});