语法错误,codeigniter中意外的'protected'(T_PROTECTED)

时间:2017-02-21 06:26:23

标签: php codeigniter constructor codeigniter-3

Admin_controller

<?php
class Admin_controller extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model("Adminmodel","",true);

        protected $headerview = 'headerview';
        protected function render($content) { 
            //$view_data = array( 'content' => $content);
            $this->load->view($this->headerview);
        }
     }
}
?>

我想在所有应用程序页面中访问我的headerview.php,这样我就像上面那样创建了它,但它显示了像 Parse error: syntax error, unexpected 'protected' (T_PROTECTED) in C:\xampp\htdocs\ci3\application\controllers\admin\Admin_controller.php这样的错误。 怎么解决这个问题?

2 个答案:

答案 0 :(得分:2)

您不是假设使用访问修饰符在构造函数内创建/声明函数,否则会抛出与您相同的错误。你可以创建匿名函数或普通函数声明,考虑一下:

class Student {

  public function __construct() {

    // below code will run successfull
    function doingTask () {
       echo "hey";    
    }
    doingTask();

    // but this will throw an error because of declaring using access modifier
    public function doingTask () {
       echo "hey";    
    }
  }
}

$std = new Student;

答案 1 :(得分:1)

  

无法在__construct

中创建功能
class Admin_controller extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model("Adminmodel","",true);

        $headerview = 'headerview';
        $this->render($headerview); # calling render() function in same class

    }

    function render($content) 
    { 
        //$view_data = array( 'content' => $content);
        $this->load->view($this->headerview);
    }
}