Php 如何在抽象类中使用Codeigniter实例?
我在Codeigniter中创建了一个标准类Loadercontentcontroller。 同样在同一文件中,添加抽象搜索类和子类SearchDoctor: 当我尝试获取CI实例时&get_instance;在抽象构造函数中,我得到NULL:Php 如何在抽象类中使用Codeigniter实例?,php,codeigniter,Php,Codeigniter,我在Codeigniter中创建了一个标准类Loadercontentcontroller。 同样在同一文件中,添加抽象搜索类和子类SearchDoctor: 当我尝试获取CI实例时&get_instance;在抽象构造函数中,我得到NULL: $this->_ci =&get_instance(); var_dump($this->_ci); // NULL 所以在我不能使用之后: $this->_ci->load->view($this->vie
$this->_ci =&get_instance();
var_dump($this->_ci); // NULL
所以在我不能使用之后:
$this->_ci->load->view($this->view, $this->item);
因为$this->\u ci是空的。CI中抽象类的代码有什么问题?也许你可以问一些关于代码的问题?错了吗
完整代码:
<?php
abstract class Search
{
protected $fields = array('country' => 'country', 'city' => 'city');
protected $table;
protected $limit = 20;
protected $offset = 0;
protected $items;
protected $rand = false;
protected $data;
protected $view;
protected $_ci;
public function __construct($input)
{
$this->_ci =&get_instance();
$this->data = $input;
$this->getPostGet();
}
public function getPostGet(){
if(empty($this->data)){
foreach($this->data as $key => $val){
$this->_check($val, $key);
}
}
}
public function _check($val, $key){
if(in_array($val, $this->fields)){
$this->items[] = $this->getField($val, $key);
}
}
public function getField($value, $key){
return $key. ' = '.$value;
}
public function doQuery(){
if(!empty($this->items)){
$limit = $this->formatLimit();
$this->items = $this->other_model->getItemsTable($this->query, $this->table, $limit);
}
}
public function formatLimit(){
$this->offset = (isset($this->data['limit'])) ? $this->data['limit'] : $this->offset;
$this->limit = (isset($this->data['limit'])) ? $this->offset + $this->limit : $this->limit;
return array('offset' => $this->limit, 'limit' => $this->limit);
}
public function addFields($array){
$this->fields = array_merge($this->fields, $array);
}
public function getView(){
$this->_ci->load->view($this->view, $this->item);
}
public function response(){
echo json_encode($this->view); die();
}
}
class SearchDoctor extends Search
{
protected $fields = array('name' => 'DetailToUsersName');
public function __construct($type)
{
$this->table = 'users';
$this->view = 'users/doctors_list';
$this->limit = 10;
$this->rand = true;
$this->addFields($this->fields);
parent::__construct($type);
}
}
/* Init class */
class Loadercontent {
public function index(){
if(isset($_GET) || isset($_POST)){
$class = 'Search';
if(isset($_POST['type'])){
$type = $_POST['type'];
$input = $_POST;
}
if(isset($_GET['type'])){
$type = $_GET['type'];
$input = $_GET;
}
$class = $class.$type;
if (class_exists($class)) {
$obj = new $class($input);
$obj->getView();
$obj->response();
}
}
}
}
?>
您好,我刚刚对您的代码进行了一些简化测试,我只使用了变量$\u ci,并调用了方法getView,它对我来说非常有效!!,但是如果您仍然遇到这个问题,为什么不将上下文传递给类构造函数new$class$input,$this非常感谢,将再次检查。关于clear class,你能说些什么?这是个好工作还是需要改变?可以使用单例模式吗?目标是为不同的内容创建分页。它无法工作,因为Loadercontent未由CI_ControllerOh扩展!我没有注意到这一点,对不起,我的不好,如果get_实例返回null,则表示CI实例尚未加载!你是否正在尝试创建某种类型的文档?如果是,请检查文档,并确保遵守其规范