stuartmarsh Posted August 29, 2008 Share Posted August 29, 2008 Does anybody know if it is possible to create new variables/functions on the fly in a parent class and have it pass down to all created children? From my experience only the last created child will receive the change. Take a look at this code to see what I mean: <?PHP class Dad { private static $instance; function __construct() { self::$instance =& $this; } public static function &get_instance() { return self::$instance; } function foo() { echo "foo<br />"; } } function &get_instance() { return Dad::get_instance(); } class Child1 extends Dad { function bar1() { echo "Bar1<br />"; } } class Child2 extends Dad { function bar2() { echo "Bar2<br />"; } } $Kid =& new Child1(); $Kid->foo(); $Kid->bar1(); $Kid2 =& new Child2(); $Kid2->foo(); echo "<hr>"; $base =& get_instance(); $base->newVar = "Bar"; var_dump($Kid); var_dump($Kid2); ?> When I add newVar to the parent class, it is only passed to $Kid2. How can I make it so that both $Kid and $Kid2 get the new variable? Is it even possible? Link to comment https://forums.phpfreaks.com/topic/121887-solved-passing-from-parent-class-to-all-children/ Share on other sites More sharing options...
stuartmarsh Posted September 1, 2008 Author Share Posted September 1, 2008 Can anyone help me with this? I've been reading up on abstract classes and interfaces but not really sure if they would help or how to use them. Link to comment https://forums.phpfreaks.com/topic/121887-solved-passing-from-parent-class-to-all-children/#findComment-630894 Share on other sites More sharing options...
coder_ Posted September 1, 2008 Share Posted September 1, 2008 Take a look at this code. I have just wrote it.. <?php class ParentClass { static protected $objects; public function __construct() { self::$objects = array(); } public function __get($name) { if (key_exists($name, self::$objects)) { return self::$objects[$name]; } } public function __set($name, $value) { self::$objects[$name] = $value; } } class ChildClass extends ParentClass { public function __construct() { parent::__construct(); } public function __get($name) { if (key_exists($name, parent::$objects)) { return parent::$objects[$name]; } } public function __set($name, $value) { parent::$objects[$name] = $value; } } $parent = new ParentClass(); $child = new ChildClass(); $secondChild = new ChildClass(); $parent->user = "c0d3r"; echo $child->user; echo $secondChild->user; ?> This is doing exactly what you want... Link to comment https://forums.phpfreaks.com/topic/121887-solved-passing-from-parent-class-to-all-children/#findComment-630933 Share on other sites More sharing options...
stuartmarsh Posted September 1, 2008 Author Share Posted September 1, 2008 This looks very promising!! I'm going to have a play with it later. Could you tell me if this would allow me assign objects to $parent? I.e $parent->user = new class Link to comment https://forums.phpfreaks.com/topic/121887-solved-passing-from-parent-class-to-all-children/#findComment-631357 Share on other sites More sharing options...
keeB Posted September 2, 2008 Share Posted September 2, 2008 What you want to implement is an Observer Pattern[1]. Here's a reference implementation in Python. It should port over quite easily for PHP. If you need any assistance or have any additional questions, I'll be happy to answer them. [1]: http://en.wikipedia.org/wiki/Observer_Pattern#Python Link to comment https://forums.phpfreaks.com/topic/121887-solved-passing-from-parent-class-to-all-children/#findComment-631699 Share on other sites More sharing options...
stuartmarsh Posted September 2, 2008 Author Share Posted September 2, 2008 keeB: Thanks for the link. The code provided by coder_ seems to work along those principles. I should be able to integrate it into my existing project quite easily. Link to comment https://forums.phpfreaks.com/topic/121887-solved-passing-from-parent-class-to-all-children/#findComment-631907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.