我想用一些函数扩展/system/core/Log.php
库。一个函数应该通过自定义函数sendMail()
发送错误,该函数是Custom_library.php
的一部分。
因此,我创建了文件/application/core/MY_Log.php
class MY_Log extends CI_Log {
public function __construct()
{
parent::__construct();
}
public function write_log($level, $msg)
{
$result = parent::write_log($level, $msg);
return $result;
}
}
问题:我无法加载Custom_library.php
。这些方法都不起作用:
//approach 1
$this->load->library('Custom_library');
//approach 2
$CI =& get_instance();
$CI->load->library('Custom_library');
方法2的错误消息:
Fatal error: Uncaught Error: Class 'CI_Controller' not found in /home/gp/public_html/system/core/CodeIgniter.php:366 Stack trace: #0 /home/gp/public_html/application/core/MY_Log.php(13): get_instance() #1 /home/gp/public_html/system/core/Common.php(478): MY_Log->write_log('error', 'Severity: error...') #2 /home/gp/public_html/system/core/Exceptions.php(105): log_message('error', 'Severity: error...') #3 /home/gp/public_html/system/core/Common.php(662): CI_Exceptions->log_exception('error', 'Exception: Clas...', '/home/gp/public...', 366) #4 [internal function]: _exception_handler(Object(Error)) #5 {main} thrown in /home/gp/public_html/system/core/CodeIgniter.php on line 366
问题:是否可以在核心类中加载和使用库?
我通过函数&load_class
尝试了这种方法。
function &load_class($class, $directory = 'libraries', $param = NULL)
{
static $_classes = array();
// Does the class exist? If so, we're done...
if (isset($_classes[$class]))
{
return $_classes[$class];
}
$name = FALSE;
// Look for the class first in the local application/libraries folder
// then in the native system/libraries folder
foreach (array(APPPATH, BASEPATH) as $path)
{
if (file_exists($path.$directory.'/'.$class.'.php'))
{
$name = 'CI_'.$class;
if (class_exists($name, FALSE) === FALSE)
{
require_once($path.$directory.'/'.$class.'.php');
}
break;
}
}
// Is the request a class extension? If so we load it too
if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
{
$name = config_item('subclass_prefix').$class;
if (class_exists($name, FALSE) === FALSE)
{
require_once(APPPATH.$directory.'/'.$name.'.php');
}
}
// Did we find the class?
if ($name === FALSE)
{
// Note: We use exit() rather than show_error() in order to avoid a
// self-referencing loop with the Exceptions class
set_status_header(503);
echo 'Unable to locate the specified class: '.$class.'.php';
exit(5); // EXIT_UNK_CLASS
}
// Keep track of what we just loaded
is_loaded($class);
$_classes[$class] = isset($param)
? new $name($param)
: new $name();
return $_classes[$class];
}
如果我运行函数$CI =& load_class('Custom_library');
,它会找到正确的文件,而不是寻找带有前缀" CI _"的类。抛出错误信息:
Fatal error: Uncaught Error: Class 'CI_Custom_library' not found in /home/gp/public_html/system/core/Common.php:196 Stack trace: #0 /home/gp/public_html/application/core/MY_Log.php(14): load_class('Custom_library') #1 /home/gp/public_html/system/core/Common.php(478): MY_Log->write_log('error', 'Severity: error...') #2 /home/gp/public_html/system/core/Exceptions.php(105): log_message('error', 'Severity: error...') #3 /home/gp/public_html/system/core/Common.php(662): CI_Exceptions->log_exception('error', 'Exception: Clas...', '/home/gp/public...', 196) #4 [internal function]: _exception_handler(Object(Error)) #5 {main} thrown in /home/gp/public_html/system/core/Common.php on line 196
答案 0 :(得分:2)
如果您需要访问其他库,扩展CI_Log
将不起作用。原因是在CI_Log
创建之前很久就创建了$CI
,因此&get_instance()
没有“实例”可供返回。
$this->load
不起作用,因为$this
不是控制器($this
且$CI
指向同一个对象)和类load
(' CI_Loader')尚未创建。
这可能有不止一种方法。在我看来,最不被黑客攻击的方法是让您的记录器类使用CI_Log
代替extend
。
<强>应用/库/ Logger.php 强>
class Logger
{
protected $CI;
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->library('custom_library');
}
public function write_log($level, $msg)
{
//do stuff with "custom_library"
$this->CI->custom_library->some_function();
//use the built-in logging mechanism, a.k.a. CI_Log
return log_message($level, $msg);
}
}
您的`logger'需要在Controller中加载,与其他库一样。
$this->load->library('logger');
用法示例可能是这样的
$this->logger->write_log('error', "This is FUBAR");
当您致电$this->load->library('logger');
时,log
类已创建并且属于$CI
(a.k.a。$this
)。所以这一行
//use the built-in logging mechanism, a.k.a. CI_Log
return log_message($level, $msg);
可以这样做而不是
//use the built-in logging mechanism, a.k.a. CI_Log
return $this->CI->log->write_log($level, $msg);
这会稍微提高效率,因为无论如何log_message
所做的就是调用log->write_log
。我没有看到任何问题,而不是使用log_message
。
有趣的问题,我通过研究了解了很多。感谢。