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? Link to comment https://forums.phpfreaks.com/topic/76396-solved-calling-variables-in-an-extended-class/ 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; } } ?> Link to comment https://forums.phpfreaks.com/topic/76396-solved-calling-variables-in-an-extended-class/#findComment-386825 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() ? Link to comment https://forums.phpfreaks.com/topic/76396-solved-calling-variables-in-an-extended-class/#findComment-386836 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. Link to comment https://forums.phpfreaks.com/topic/76396-solved-calling-variables-in-an-extended-class/#findComment-386839 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) Link to comment https://forums.phpfreaks.com/topic/76396-solved-calling-variables-in-an-extended-class/#findComment-386848 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? Link to comment https://forums.phpfreaks.com/topic/76396-solved-calling-variables-in-an-extended-class/#findComment-386867 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 Link to comment https://forums.phpfreaks.com/topic/76396-solved-calling-variables-in-an-extended-class/#findComment-386900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.