2

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?

Community
  • 1
  • 1
Kevin
  • 653
  • 2
  • 13
  • 34

2 Answers2

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;
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
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);
    }
}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85