tags:

views:

12

answers:

3

Hi Guys,

I am having problems creating a variable with data from another class. Here is what I am doing...

Please let me know you think.

 <?PHP
  class Customers extends Controller {

      private $foo = $this->session->userdata('foo');
  }

Thanks, Peter

+2  A: 

You probably want something more like this:

class Customers extends Controller 
{
  private $foo;
  public function __construct()
  {
    parent::__construct();
    $this->foo = $this->session->userdata('foo');
  }
}

It's hard to know for sure without knowing more about your project.

konforce
Correct in principle, but `$this->session` can't be an initialized object at that point yet....
Pekka
This worked perfectly!
Peter
@Pekka, That's why I'm calling the parent's constructor, as that's the only way it could possibly exist at that point.
konforce
@konforce hah, sneaky edit! :)
Pekka
A: 

This is not possible: $this doesn't exist at the moment when you define the class, and you can't call functions at all at this point at all.

You will need to assign $foo in the constructor, after $this->session has been initialized. (@konforce beat me to the example.)

Pekka
+1  A: 

You can set it with constructor because you are inhering from parent class:

class Customers extends Controller {
  private $foo = null;

  function __construct(){
     parent::__construct();
     $this->foo = $this->session->userdata('foo');
  }
}
Sarfraz
This worked perfectly. Thank you!
Peter
@Peter: Welcome :)
Sarfraz