views:

109

answers:

6

Lets say I have a parent class

class parent { }

.....

This parent has three sub class

class child1 { }

class child2 { }

class child3 { }

and these child classes have further smaller parts like

class child1subpar1 { }

class child1subpar2 {

       public function foo() {
          echo "hi";
       }

}

class child2subpar1 { }

class child2subpar2 { }

Now, how to sum this whole up like

class child1 extends child1subpar1, child1subpar2 { }
class child2 extends child2subpar1, childsubpar1 { }

class parent extends child1,child2,child3 { }

I need to execute the methods in its inherited classes and do something like this

$objparent = new parent; $objparent -> foo();

A: 

No, but multiple inheritance is generally considered a bad practice. You should favor composition instead, so you just use instances of classes you wanted to inherit inside your class.

And now when I look into your question again, it's not even an inheritance issue, you should use composition. Maybe if you provided more detail what you expect that class to do, we should answer more accurately.

UPDATE:

You will need to create one method for each of these classes' method which you would want to use - it's called Facade design pattern. Or maybe you are not aware that you can call methods of inner objects like this:

$parent->subObject->subSubObject->wantedMethod();

Facade pattern:

http://en.wikipedia.org/wiki/Facade_pattern

in your case facade wouldn't be anything else than creating one class and define every single method you want to use, and inside that method calling any method of any of your inner instances. But i really don't see any benefit coming from this instead of calling instances and methods hierarchically

praksant
you mean to say execute the methods of different classes seperately by creating seperate objects?
Starx
I'm not sure what your problem is, because your question isn't very specific. But i'm saying you should create instances of what you call subclasses inside your parent object, and call methods as needed. I don't think i can be more useful without you better explaining your problem
praksant
all the subclasses and actually part which make up bigger class just like a "webpage" contains "head", "body" and body contains banner, content, footer, and banner contains flashheader, menubar . And I am trying to create a instance of webpage class and trying to show the whole webpage in one blow
Starx
I am actually doing the way you are suggesting, just trying to find a better option
Starx
well, this way doesn't lead to any better option :) I made an update to my answer, but i'm not sure if it would be of any help. And also you shouldn't be making bigger and bigger classes, you should make each class relatively simple, and distribute responsibility to smaller parts. It looks like you're doing just the oposite
praksant
ok, I think that can solve it, can you post a basic example of facade design pattern.
Starx
+2  A: 

Seems like you're really confused about OOP.

Parent class has no awareness of its children. If you want to execute a child class, you need to create its instance.

Multiple inheritance is also not allowed in PHP (as well as many other popular languages like Java).

It might be worth looking at aggregation - passing smaller sub classes into child or event parent class. also, you can use implement multiple interfaces to force subclasses to have a set of required methods.

Ilya Biryukov
+1 Agree - "you can use implement multiple interfaces to force subclasses to have a set of required methods"
falomir
but I want to execute all methods using one instance of the parent class
Starx
@starx, sorry mate, it's impossible. Invent your own language :D
Ilya Biryukov
I think I am way too amateur for that kind of thing right now?
Starx
It's definitely a good thing you are looking for better solutions. This way you will learn and grow ;)
praksant
+1  A: 

What you're doing is really backwards. Inheritance is used to bestow common, shared functionality upon objects without code duplication. The inheritance goes from Parent to Child, everything the Parent can do, the Child can do as well, but it may do more (it extends the functionality of the parent).

class Parent {

    function everyoneCanDoThis() { }

}


class Child extends Parent {

    // I can implicitly use the everyoneCanDoThis() function

    function onlyChildrenCanDoThis() { }

}

Since this is a top-down structure, the Parent should not rely on any specific Child. The Parent does not execute or call functions of a Child. Only you call functions of a Child, but these functions may be inherited from a Parent class.

You should put everything you want every object to be able to do in a Parent class. Specific functionality that's only relevant to a specific object goes into a Child.

Multiple inheritance is a different can of worms that's not possible in PHP, for good reasons. Come back to composition, as suggested elsewhere here, when you get the basics of inheritance. :)


Composition just means that you take several objects and hold references to them in another object. It has nothing to do with inheritance, as each of these objects may or may not inherit from a Parent class and they're still individual objects.

class ComposedObject {

    private $part1 = null;
    private $part2 = null;

    public function __constructor() {
        $this->part1 = new Part1();
        $this->part2 = new Part2();
    }

    public function doTask() {
        return $this->part1->doSomeTask();
    }

    public function doOtherTask() {
        return $this->part2->doSomeOtherTask();
    }

}

The ComposedObject does not have any functionality itself, inherited or otherwise. But it holds two other objects that each carry some functionality. The functionality in the "parts" may be exposed directly, so they're called like $composedObject->part1->doSomeTask(), they may be encapsulated as in the example, so they're called like $composedObject->doTask() and are internally delegated, or you may use some __call() trickery to automatically delegate functions called on the composed object to one of its "parts". This has the same problem as multiple inheritance though; if two "parts" both contain a method of the same name, which one do you call?

deceze
Can you explain me a litte bit about composition?
Starx
@Starx I said come back later, oh well... ;o) Updated with an example of composition.
deceze
A: 

A class can implement more than one interface, which is a slightly different thing.

When you inherit from a parent, you get everything it has unless you choose to override something, plus you can extend it by adding additional more specific stuff, but the parent should not know anything about the child.

When you implement an interface, the interface defines methods but it doesn't implement them. It is up to you to implement it. Different classes can implement the interface methods however they want as long as they follow what the interface says.

Inheritance tends to be overused and leads to bad programs. Perhaps if you told us what problem you're trying to solve, one of us could suggest how you can structure your classes.

A: 

Using Interface in Php maybe solve the question.

zhangwenjie
+1  A: 

+1 to the others. You really have it backwards. Children ALWAYS extends their parents.

But there is even something that can act kind of like multiple inheritance in PHP: The Decorator Pattern. I wrote an article about it on my blog here.

Techpriester