CodeIgniter 父声明

CodeIgniter Parent Declare

如何在我的 parent::_construct 中说下面这段代码,然后将其调用到单独的子函数中?

      /*bootstrap classes*/
      $pag_config ['full_tag_open'] = "<ul class='pagination'>" ;
      $pag_config ['full_tag_close'] ="</ul>" ;
      $pag_config ['num_tag_open'] = '<li>' ;
      $pag_config ['num_tag_close'] = '</li>' ;
      $pag_config ['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>" ;
      $pag_config ['cur_tag_close'] = "<span class='sr-only'></span></a></li>" ;
      $pag_config ['next_tag_open'] = "<li>" ;
      $pag_config ['next_tagl_close'] = "</li>" ;
      $pag_config ['prev_tag_open'] = "<li>" ;
      $pag_config ['prev_tagl_close'] = "</li>" ;
      $pag_config ['first_tag_open'] = "<li>" ;
      $pag_config ['first_tagl_close'] = "</li>" ;
      $pag_config ['last_tag_open'] = "<li>" ;
      $pag_config ['last_tagl_close'] = "</li>" ;

父结构内部:

     $this -> pag_config = array (
        /*bootstrap classes*/
        'full_tag_open' => "<ul class='pagination'>" ,
        'full_tag_close' => "</ul>" ,
        'num_tag_open' => '<li>' ,
        'num_tag_close' => '</li>' ,
        'cur_tag_open' => "<li class='disabled'><li class='active'><a href='#'>" ,
        'cur_tag_close' => "<span class='sr-only'></span></a></li>" ,
        'next_tag_open' => "<li>" ,
        'next_tagl_close' => "</li>" ,
        'prev_tag_open' => "<li>" ,
        'prev_tagl_close' => "</li>" ,
        'first_tag_open' => "<li>" ,
        'first_tagl_close' => "</li>" ,
        'last_tag_open' => "<li>" ,
        'last_tagl_close' => "</li>" ,
      ) ;

内部子函数:

      $pag_config ['total_rows'] = count ($to_count) ;
      $pag_config ['per_page'] = $to_show ; 
      $pag_config ['uri_segment'] = 3 ; 
      $pag_config ['base_url'] = base_url().'prcsys/overall' ;

      $merge_paginate = array_merge($pag_config,$this -> pag_config);
      $this -> pagination -> initialize ($merge_paginate) ;

使用 $this 访问 class 个变量。

class YourClass extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->pag_config = array();
        $this->pag_config['full_tag_open'] = "<ul class='pagination'>" ;
        $this->pag_config['full_tag_close'] ="</ul>" ;
        $this->pag_config['num_tag_open'] = '<li>' ;
        $this->pag_config['num_tag_close'] = '</li>' ;
        $this->pag_config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>" ;
        $this->pag_config['cur_tag_close'] = "<span class='sr-only'></span></a></li>" ;
        $this->pag_config['next_tag_open'] = "<li>" ;
        $this->pag_config['next_tagl_close'] = "</li>" ;
        $this->pag_config['prev_tag_open'] = "<li>" ;
        $this->pag_config['prev_tagl_close'] = "</li>" ;
        $this->pag_config['first_tag_open'] = "<li>" ;
        $this->pag_config['first_tagl_close'] = "</li>" ;
        $this->pag_config['last_tag_open'] = "<li>" ;
        $this->pag_config['last_tagl_close'] = "</li>" ;
    }

    function childFunction() {
        echo $this->pag_config['full_tag_open'];
    }

}

上面的示例与您要求的更相似,但我建议将其定义为 属性,因为它看起来更简洁。

class YourClass extends CI_Controller {

    private $pag_config = array (
        /*bootstrap classes*/
        'full_tag_open' => "<ul class='pagination'>" ,
        'full_tag_close' => "</ul>" ,
        'num_tag_open' => '<li>' ,
        'num_tag_close' => '</li>' ,
        'cur_tag_open' => "<li class='disabled'><li class='active'><a href='#'>" ,
        'cur_tag_close' => "<span class='sr-only'></span></a></li>" ,
        'next_tag_open' => "<li>" ,
        'next_tagl_close' => "</li>" ,
        'prev_tag_open' => "<li>" ,
        'prev_tagl_close' => "</li>" ,
        'first_tag_open' => "<li>" ,
        'first_tagl_close' => "</li>" ,
        'last_tag_open' => "<li>" ,
        'last_tagl_close' => "</li>" ,
    ) ;

    function childFunction() {
        echo $this->pag_config['full_tag_open'];
    }

}