syntax error, unexpected 'protected' (T_PROTECTED) in codeigniter

12,110

Solution 1

There is no way to create function inside the __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);
    }
}

Solution 2

You're not supposing creating/declaring a function inside contructor with access modifier,otherwise it will throws an errors like you did. You can create anonymous function or normal function declaration instead, consider this :

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;
Share:
12,110
Kevin
Author by

Kevin

As a MEAN stack developer I am developing website using node.js, angular.js, mongoDB, and express.js. I have 2.3 years of experience in website developing.

Updated on June 04, 2022

Comments

  • Kevin
    Kevin almost 2 years

    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);
            }
         }
    }
    ?>
    

    I want to access my headerview.php in all pages of application so that I have created like above but it showing error like Parse error: syntax error, unexpected 'protected' (T_PROTECTED) in C:\xampp\htdocs\ci3\application\controllers\admin\Admin_controller.php. How to solve this?