noidtluom Posted November 7, 2007 Share Posted November 7, 2007 I'm trying to do this: <?php class foo { protected $asdf; public function __construct() { $this->asdf = 'lol'; } } class bar extends foo { public function __construct() { echo $this->asdf; } } ?> How can it be done? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 code for u <?php class foo { protected $asdf; public function __construct() { $this->asdf = 'lol'; } } class bar extends foo { public function __construct() { parent::__construct(); echo $this->asdf; } } ?> Quote Link to comment Share on other sites More sharing options...
noidtluom Posted November 7, 2007 Author Share Posted November 7, 2007 So you mean that whatever variables you set in class foo can only be called in the extended class bar if you do parent::whateverFunctionYouSetTheVariableIn() ? Quote Link to comment Share on other sites More sharing options...
trq Posted November 7, 2007 Share Posted November 7, 2007 No. But you set asdf within the __construct of the parent, but the parents __construct is not called automatically when a child is instantiated. If you simply had... <?php class foo { protected $asdf = 'lol'; } class bar extends foo { public function __construct() { echo $this->asdf; } } ?> That would work fine. Quote Link to comment Share on other sites More sharing options...
noidtluom Posted November 7, 2007 Author Share Posted November 7, 2007 Ok. Thanks. Glad I sorted out those principles. Alright, so how would I get this to work? <?php class foo { protected $asdf; public function test($var) { $this->asdf = $var; } } class bar extends foo { public function anothertest() { echo $this->asdf; } } $foo = new foo(); $foo->test('lol'); $bar = new bar(); $bar->anothertest(); ?> (It's a simplified version of the code I'm working on) Quote Link to comment Share on other sites More sharing options...
aschk Posted November 7, 2007 Share Posted November 7, 2007 I think you misunderstand inheritance. There you have created 2 DIFFERENT objects, one of type foo and another of type bar. Inside foo there is a variable called asdf, by calling test('lol') you are setting asdf inside foo ONLY to 'lol'. Now inside bar you also have a variable called asdf, which when you do $bar = new bar() is set to nothing (null), you then call anothertest() which grabs that internal variable from $bar and echo's it. It's value is null there nothing is output. What you really want to do is : $bar = new bar(); $bar->test('lol'); $bar->anothertest(); How does this work? the bar class inherited the method test(). Understand any better now? Quote Link to comment Share on other sites More sharing options...
noidtluom Posted November 7, 2007 Author Share Posted November 7, 2007 Ah yes! Thank you! I'm understanding it a lot better now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.