如何在抽象类中使用Codeigniter实例?

时间:2015-01-28 20:49:50

标签: php codeigniter

我在Codeigniter中创建了一个标准类Loadercontent(控制器)。 同样在同一文件中添加abstract Search类和子类SearchDoctor

当我尝试在抽象构造函数中获取CI实例&get_instance();时,我得到NULL:

$this->_ci =&get_instance();
var_dump($this->_ci); // NULL

所以在我不能使用之后:

$this->_ci->load->view($this->view, $this->item);

因为$this->_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();
            }
        }
    }
}

?>

0 个答案:

没有答案