chocopi Posted November 12, 2009 Share Posted November 12, 2009 Is there anyway for two (or more) child classes to inherit a parent class with there only be one iteration of the parent class. What I mean is, if child class 1 changes one of the parent properties I would like child class 2 to have access to that variable. Obviously when you inherit the parent class a new object is created therefore causing both the child classes access to clones of the parent. I know that i could global the parent object within each of the child methods, but seems like poor coding standards to me I've had a quick check on google, php.net and these forums, but I'm not really sure what to search for :-\ I'm sure there is a very simple solution to this which I have overlooked Any help will be greatly appreciated ps. In case it matters I'm using php version 5.3.0 Link to comment https://forums.phpfreaks.com/topic/181272-solved-classes-and-inheritance/ Share on other sites More sharing options...
Garethp Posted November 12, 2009 Share Posted November 12, 2009 http://www.google.com.au/search?hl=en&rlz=1B3GGGL_en-GBAU349AU349&ei=6Vb8SqPxJ4_s6gOAspCaAg&sa=X&oi=spell&resnum=0&ct=result&cd=1&ved=0CA4QBSgA&q=class+extend+php&spell=1 Link to comment https://forums.phpfreaks.com/topic/181272-solved-classes-and-inheritance/#findComment-956291 Share on other sites More sharing options...
xangelo Posted November 12, 2009 Share Posted November 12, 2009 I am not 100% on this, but the static keyword would preserve an items state throughout instances.. Link to comment https://forums.phpfreaks.com/topic/181272-solved-classes-and-inheritance/#findComment-956310 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 static variables are shared between subclasses. http://www.php.net/manual/en/language.oop5.static.php#89650 the example on the page <?php class MyParent { protected static $variable; } class Child1 extends MyParent { function set() { self::$variable = 2; } } class Child2 extends MyParent { function show() { echo(self::$variable); } } $c1 = new Child1(); $c1->set(); $c2 = new Child2(); $c2->show(); // prints 2 ?> Link to comment https://forums.phpfreaks.com/topic/181272-solved-classes-and-inheritance/#findComment-956321 Share on other sites More sharing options...
chocopi Posted November 12, 2009 Author Share Posted November 12, 2009 Thanks for the replies. I've just been a bit of testing with the static and your right Once i re-read the static page and realised i needed to use self::$var instead of $this->var it became much simplier. Edit: Seeing how mikesta707 has replied while I was, i nearly missed it I must remember to read the user notes on the php.net, sorry for wasting your time That was laziness on my part Anyways Thanks [ SOLVED ] Link to comment https://forums.phpfreaks.com/topic/181272-solved-classes-and-inheritance/#findComment-956332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.