Jump to content

Problem with objects


realshadow

Recommended Posts

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 :P)

Here is the code.

 

foreach($objects as $object) {
  $object = new $object;	
}

 

Which will result in following.

 

adfs.jpg

 

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

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?  :P

 

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.