realshadow Posted November 23, 2008 Share Posted November 23, 2008 Hello. I am trying to figure out how to call a certain class (main class) so it will have all methods from extended classes. (I hope you understand what I am trying to do ) Here is the code. foreach($objects as $object) { $object = new $object; } Which will result in following. That means the main object will only have his methods and methods from the last class in the list. Not all I am kinda stupid, I know. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/133906-problem-with-objects/ Share on other sites More sharing options...
DarkWater Posted November 23, 2008 Share Posted November 23, 2008 I don't really understand what you mean. o_O Want to elaborate? Link to comment https://forums.phpfreaks.com/topic/133906-problem-with-objects/#findComment-697073 Share on other sites More sharing options...
realshadow Posted November 23, 2008 Author Share Posted November 23, 2008 Ok. Lets say I have. class a { // methods and stuff } class b extends a { // methods and stuff } Now when I want to call a method from class a, I need to initialize it with $a = new a; And then I can call methods by $a->somemethod(); Same goes for class b. BUT I want to be able to call all methods from class b the same way I call them with class a. So if I have 100000 classes that extend class a, I can call each of them like this $a->blabla(); Do you copy? In the picter is what will I get with get_class_methods()h, where I have three classes that extend the main class. And as you can see when it ends (foreach loop), I get only methods from the last class. I hope you understand it now. Link to comment https://forums.phpfreaks.com/topic/133906-problem-with-objects/#findComment-697157 Share on other sites More sharing options...
corbin Posted November 23, 2008 Share Posted November 23, 2008 foreach($objects as $object) { $object = new $object; } Things are not by reference in foreach. That object will never be that value again. $numbers = array(1,2,3,4,5,6,7); foreach($numbers as $number) { $number = 0; } print_r($numbers); The output would be 1,2,3,4,5,6,7. Link to comment https://forums.phpfreaks.com/topic/133906-problem-with-objects/#findComment-697278 Share on other sites More sharing options...
xdracox Posted November 27, 2008 Share Posted November 27, 2008 Derived classes always inherit public and protected members from their base classes. Example: class A { public function aMethod() { echo 'A::aMethod()!'; } } class B extends A { public function bMethod() { echo 'B::bMethod()!'; } } $obj = new B; $obj->aMethod(); $obj->bMethod(); Output: A::aMethod()! B::bMethod()! Link to comment https://forums.phpfreaks.com/topic/133906-problem-with-objects/#findComment-700086 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.