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?


Messages In This Thread
Accessing controller public variables from multiple methods - by El Forum - 03-31-2011, 05:45 PM



Theme © iAndrew 2016 - Forum software by © MyBB