Liquid Fire Posted July 16, 2007 Share Posted July 16, 2007 I know that php classes are not exactly the same as other languages classes(like C++) so i have a question. Let say i have this code: <?php class a { protected function test() { print_r($this->_data_map); } protected $_data_map; } class b extends a { public function __contruct() { parent::$_data_map = array("a" => "b"); } public function test() { parent::test(); } } $a = new a(); $b = new b(); $b->test(); ?> My question is will class a have access to the data created by class b's constructor? Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 OK, I figured it out. First.. public function __contruct() Should be public function __construct() That was annoying! I usually use the name of the class for the constructor. In this case, it would be class a { public function a() {} Anyway.. You forgot to declare the functions static, since you're accessing them in a static way. This works: <?php class a { protected static function test() { print_r(self::$_data_map); } protected static $_data_map; } class b extends a { public function __construct() { parent::$_data_map = array("a" => "b"); } public static function test() { parent::test(); } } $a = new a(); $b = new b(); $b->test(); ?> Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted July 16, 2007 Author Share Posted July 16, 2007 no, i don't want it to be static. I want every class that extends from this class to have its own one(not every class is going ot have the same data map). If it has to be static then i will have to declare in it each class. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted July 16, 2007 Share Posted July 16, 2007 I don't believe parent classes (in your example, class a) can have knowledge of their child classes (class b). Child classes, however, can have knowledge (properties and methods) of their parent class. So, if you have a super class that is the basis of all your datamap stuff, the child classes will have the same types of properties and methods as those that are either public or protected in the parent class. So: <?php class DataMapper{ protected $dataMap; protected function test(){ print_r($this->dataMap); } } class UniqueMapper extends DataMapper{ public function __construct(){ $this->dataMap = array("a" => "b"); } } class ComplexMapper extends DataMapper{ public function __construct($key, $value){ $this->dataMap = array($key => $value); } } $unique = new UniqueMapper(); $complex = new ComplexMapper("name", "Bubba"); $unique->test(); //prints a => b $complex->test(); //prints name => Bubba ?> In this example, even though all classes have a $dataMap property, it's not the same exact one. Extending a class/using inheritence doesn't copy data. It just tells the child class that part of its mold came from the parent class. Quote Link to comment Share on other sites More sharing options...
keeB Posted July 16, 2007 Share Posted July 16, 2007 I'm pretty sure Nightslyr is correct in his estimation. At least, that's my experience. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 17, 2007 Share Posted July 17, 2007 keeB: You don't need methods to be static in order to use parent:: 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.