诚然,我的OOPHP有点不稳定,但我看不出这有什么问题。在我的一个控制器中,我包含了一个utils表,它和控制器一样,扩展了基本CI_Controller
类。这引发了致命错误:
Fatal error: Cannot redeclare class Utils in {file path}'utils.php on line 88
控制器:
class Dashboard extends CI_Controller {
public function __construct() {
//call parent constructor
parent::__construct();
//load utils
require 'application/helpers/utils.php'; //<-- utils loaded
$this->utils = new Utils(); //<-- utils instantiated
//load Dashboard model
$this->utils->load->model('dashboard');
}
//etc...
}
utils.php:
class Utils extends CI_Controller {
//prep for forms (on join or login views)
public function prep_form() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<li>', '</li>');
}
//etc - more util methods
}
为什么它认为我在RE声明Utils
,尽管只调用并实例化了它一次?奇怪的是,我有另一个控制器,用于网站的另一部分,具有相同的模式,它没有任何抱怨。
最后,我尝试将require
指令移动到控制器之外,并将其更改为require_once
,以防有东西真的调用了它两次,在这两种情况下,页面都挂起了,最终解决了问题,没有将源代码发送到浏览器。
提前谢谢。
重命名
helpers/utils.php
至
helpers/Utils.hp.
windows以外的服务器处理类名应与文件名匹配。
希望它能帮助你。
使用$this->load->helper((而不是require((
为什么不直接打电话给:
$this->load->library('utils'); // needs to be stored in the libraries directory
的插页
require 'application/helpers/utils.php'; //<-- utils loaded
$this->utils = new Utils(); //<-- utils instantiated
我不知道你为什么要把类从CI_Controller扩展到你的助手。助手函数的助手(如按键对MySQL结果进行排序等(。也许你需要model
?
如果您在助手处需要CodeIgniter的功能,只需使用get_instance()
而不是$this
function prep_form() {
get_instance()->load->helper('form');
get_instance()->load->library('form_validation');
get_instance()->form_validation->set_error_delimiters('<li>', '</li>');
}
尝试
require_once 'application/helpers/utils.php';
以避免此错误。
require 'application/helpers/utils.php'; //<-- utils loaded
这个可能叫两次。您可以将Dashboard
实例化两次,也可以将该文件再次包含在其他位置。