Creating a var from data created by another class
I am having problems creating a variable with data from another class. Here is what I am doing...
<?PHP
class Customers extends Con开发者_C百科troller {
private $foo = $this->session->userdata('foo');
}
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.
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');
}
}
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.)
精彩评论