Welcome Guest, Not a member yet? Register   Sign In
Accessing controller public variables from multiple methods
#1

[eluser]chuckl[/eluser]
OK, I have been tearing what little hair I have left out the past two days trying to resolve a problem and while I have narrowed the problem down considerably I am still no closer to a solution. I am using CI 2.0 with PHP5.3 on Apache 2.2. I have been coding in PHP for more years than I want to think about so this is not coming from a noob in that regard.

My problem is with public variables within a controller class. Here is my code:
Code:
class Test extends CI_Controller
{
  public $data;
    
  function __construct(){
    $this->data = array();
  }
    
  function index(){
    $this->data['test1'] = 'Test 1 - class public variable access.<br />';        
    echo 'Class index() called.<br />';
    echo $this->data['test1'];    
  }
    
  function test1(){
    $this->data['test2'] = 'Test 2 - class public variable access.<br />';    
    echo 'Class test1 called.<br />';
    echo $this->data['test1'];    
    echo $this->data['test2'];    
  }
    
  function test2()
  {
    echo 'The data array contains these two entries:<br />';
    echo $this->data['test1'];    
    echo $this->data['test2'];    
  }
}

Public variables should be updated and accessible from any function within the class and any reference external to the class...because they are public! But when I run this controller test1 errors when accessing $this->data['test1']. When I run test2 it errors on both $this->data['test1'] and $this->data['test2']. Can someone explain why this is so? What am I doing wrong or is it a bug in CodeIgniter?
#2

[eluser]InsiteFX[/eluser]
Code:
public $data = array();

// this will make all $data variables global to all views!
// vars is a global array that CodeIgniter keeps see the Loader Class.
$this->load->vars($this->data);
$this->load->view('your_view');

InsiteFX
#3

[eluser]chuckl[/eluser]
I am well aware of how it is supposed to work. The problem I am having is a bit beyond what you have outlined. Add another method/function or two that need to access or update the $data variable. The $data array elements created in one function cannot be accessed from another.
#4

[eluser]InsiteFX[/eluser]
And what happens if you echo $test1 $test2 in your functions after you set them?

InsiteFX
#5

[eluser]chuckl[/eluser]
I just changed the code to create a new Test object and call the functions. I put the following code immediately after the closing brace of the Test class:

Code:
$test = new Test;
echo $test->index();
echo $test->test1();
echo $test->test2();

When I enter the URL ../index.php/test everything works as expected. You get the following output:

Code:
Class index() called.
This is a test of class public variable access.
Class test1 called.
This is a test of class public variable access.
This is a second test of the class public variable access.
The data array contains these two entries:
This is a test of class public variable access.
This is a second test of the class public variable access.
Class index() called.
This is a test of class public variable access.

When I enter the URL ../index.php/test/test1 I get the following output:

Code:
Class index() called.
This is a test of class public variable access.
Class test1 called.
This is a test of class public variable access.
This is a second test of the class public variable access.
The data array contains these two entries:
This is a test of class public variable access.
This is a second test of the class public variable access.
Class test1 called.
A PHP Error was encountered

Severity: Notice

Message: Undefined index: test1

Filename: controllers/test.php

Line Number: 26
This is a second test of the class public variable access.

When I enter the URL ../index.php/test/test2 I get the following output:
Code:
Class index() called.
This is a test of class public variable access.
Class test1 called.
This is a test of class public variable access.
This is a second test of the class public variable access.
The data array contains these two entries:
This is a test of class public variable access.
This is a second test of the class public variable access.
Class test1 called.
A PHP Error was encountered

Severity: Notice

Message: Undefined index: test1

Filename: controllers/test.php

Line Number: 26
This is a second test of the class public variable access.

What this tells me is that it isn't a PHP problem but a CodeIgniter issue since calling the class using PHP works fine but using the CodeIgniter URI to call the functions results in an error. I am open for ideas here. Thanks.
#6

[eluser]chuckl[/eluser]
After looking at this a bit longer it appears as though each function call is getting a fresh instantiation of the controller which means a new data variable.

After more testing I am convinced that while this may be “Works As Coded” it is not the correct implementation. In system/core/CodeIgniter.php around line 267 it appears that CI instantiates a new controller object every time any controller method is called. Controllers should be instantiated as singleton objects which would allow for class variable access.

As it currently is coded class level variables will never work because you get a new instance of the class every time you call a method of the class. I guess I will have to store all of my class data values on the session stack in the meantime. Not a good thing though.
#7

[eluser]rip_pit[/eluser]
i understand better now why I can't read a controller variable from another

evertimes it breaks the whole application process Sad((


Anyway as it is a "old" post, does anybody found a trick to do that with latest CI releases ?

thanx for the explanation, i'll too store my data in another place, like in a config file or in a xml.

any suggestion is still welcome Wink




Theme © iAndrew 2016 - Forum software by © MyBB