coldkill Posted August 5, 2007 Share Posted August 5, 2007 Hello all. I'm building a new website for myself (well it's a CMS so I can basically re-use it for other projects as well). It uses classes and extended classes but I have one problem. I can't access variables from the parent class. It seems that the parent:: scope resolution operator with variables only works with PHP5 and later. My server currently runs PHP4 so it doesn't work. Anyone know how to access variables from a parent class in PHP4? I tried using functions as well and that didn't work. IE: <?php class b extends a { function b() { echo parent::aFunction(); } } ?> Thanks in advance. Cold Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/ Share on other sites More sharing options...
trq Posted August 5, 2007 Share Posted August 5, 2007 <?php class b extends a { function b() { echo a::aFunction(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/#findComment-316095 Share on other sites More sharing options...
redbullmarky Posted August 5, 2007 Share Posted August 5, 2007 hmmm as far as i'm aware (or can remember from using php 4), parent works fine in php 4. parent::method() = used to call a function that exists in parent class but has been overwritten by descendent $this->method() = call a method that has been defined in either descendent class or parent $this->variable = used to access a variable that has either been set in parent class or overwritten by descendent ref: http://uk.php.net/keyword.parent - under PHP4 OOP manual you mention variables, but your example would imply a method call - which one is it you're trying to do, and what result are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/#findComment-316124 Share on other sites More sharing options...
emehrkay Posted August 5, 2007 Share Posted August 5, 2007 to piggyback off of thorpe's answer. <?php class b extends a { function b() { echo $this->aFunction(); } } ?> edit: redbullmarky answered it Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/#findComment-316198 Share on other sites More sharing options...
coldkill Posted August 5, 2007 Author Share Posted August 5, 2007 The function works fine. I'm trying to get a variable from a parent function (one defined with the var constructor). The only way I can think of how to do this is use a function and return the value of the variable, but that didn't work when I tested it... Thanks, Cold Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/#findComment-316318 Share on other sites More sharing options...
coldkill Posted August 6, 2007 Author Share Posted August 6, 2007 parent class* not parent function. Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/#findComment-316381 Share on other sites More sharing options...
corbin Posted August 7, 2007 Share Posted August 7, 2007 class Class1 { var $a = 'foo'; function Class1() { return $this->a; } } class Class2 extends Class1 { function Class2() { echo $this->a; echo $this->Class1(); } } $c = new Class2(); //returns 'foofoo'; Does that not work? I know it works in PHP 5... Or is there a reason you're specifically trying to use the parent:: syntax? Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/#findComment-317107 Share on other sites More sharing options...
coldkill Posted August 7, 2007 Author Share Posted August 7, 2007 I think it's because I'm running PHP4. I will try that though. Thanks. Cold ::EDIT:: Tested and it works. How peculiar. Thanks for the help guys! ::SOLVED:: Quote Link to comment https://forums.phpfreaks.com/topic/63423-solved-parent-variables/#findComment-317169 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.