[eluser]chuckl[/eluser]
OK, let me try to explain it this way...basic, fundamental OO:
You can create objects by simply:
Code:
class myController extends CI_Controller
{
$data = '';
function myFunction($input){
$this->data = $input;
return $this->data;
}
function anotherFunction() {
return $this->data;
}
}
// In your view you call the function
echo $myController->myFunction('--Test 1--');
// Controller is created in CodeIgniter.php like this:
$myController = new myController;
// The controller returns and the view displays
// --Test 1--
// In your view you call the controller function
echo $myController->myFunction('--Test 2--');
// Controller is created in CodeIgniter.php like this:
$myController = new myController;
// The controller returns and the view displays
// --Test 2--
// In your view you call the controller function
echo $myController->anotherFunction();
// Controller is created in CodeIgniter.php like this:
$myController = new myController;
// The controller returns and the view displays an empty string
Please note that each object instantiation, while the same name is not the same object but a NEW object. The $data variable is a NEW variable each time.
This is what CI does whenever a controller function/method is called, it creates a NEW controller object. What should it should be doing is creating a single controller object and preventing multiple instances from being created. This is called a singleton pattern and is created using code similar to the following (in fact the CI_Controller object is created this way which means that every class that extends CI_Controller and has a parent::__construct(); call is calling the ONE and ONLY CI_Controller object):
Code:
function &getInstance;(){
static $instance;
if(!instance){
$instance = new controllerClass();
}
return $instance;
}
Under this scenario, there would be only one instance of each application controller and therefore class variables would be the same instance and could be modified or read by all methods throughout the life of the controller (Think class global variables.) Here is what would then happen:
Code:
// In your view you call the function
echo $myController->myFunction('--Test 1--');
// CI either creates a new instance if it does not exist or returns
// existing instance
// The controller returns and the view displays
// --Test 1--
// In your view you call the controller function
echo $myController->myFunction('--Test 2--');
// CI returns existing instance
// The controller returns and the view displays
// --Test 2--
// In your view you call the controller function
echo $myController->anotherFunction();
// CI returns existing instance
// The controller returns and the view displays
// --Test 2--
The current implementation results in a new object created every time a controller method is called. This is not only expensive to do it also precludes you from having class level variables. Under the current implementation the only possible way to have data persist and available for different methods within a controller is to save it off as a session variable which is not the best use of session space.
I hope you can now begin to understand the issue better.