Variable in php class which can be used in all its functions

Variable in php class which can be used in all its functions,php,class,codeigniter,scope,codeigniter-2,Php,Class,Codeigniter,Scope,Codeigniter 2,I want to declare a variable in a php class which can be used in all its functions. I am using the codeigniter framework & to have to pass data like base_url etc in each of the functions. Also I need all of such data in a single variable. For example $data['base_url']=base_url(), $data['title']="My_Site_title" etc & then send only a single variable $data (this is a common thing in codeigniter). As per this question, I modified my code to: class Search extends CI_Controller { function

I want to declare a variable in a php class which can be used in all its functions. I am using the codeigniter framework & to have to pass data like base_url etc in each of the functions. Also I need all of such data in a single variable. For example

$data['base_url']=base_url()
,
$data['title']="My_Site_title"
etc & then send only a single variable $data (this is a common thing in codeigniter).

As per this question, I modified my code to:

class Search extends CI_Controller {

function Search() {
    parent::__construct();
    $this->load->model('student_model');

    $this->load->helper('form');
    $this->load->helper('url');
    $this->output->enable_profiler(TRUE);

    $this->data['base_url']=base_url();  //gives an error here
 }

 function index() {
     $this->load->view('search_view', $this->data);
 }
 }

But still it gives an error at the marked place saying

Undefined variable: data
and
Fatal error: Cannot access empty property in ..\search.php on line 16
.


#1

Here you go:

class Search extends CI_Controller {

protected $data = array();

function Search() {
    parent::__construct();
    $this->load->model('student_model');

    $this->load->helper('form');
    $this->load->helper('url');
    $this->output->enable_profiler(TRUE);

    $this->data['base_url']=base_url();
 }

 function index() {
     $this->load->view('search_view', $this->data);
 }
 }

More about class properties in php: http://php.net/manual/en/language.oop5.properties.php


#2

can't use $data without instance