Answering your edit : If you really want to fake multiple inheritance, you can use the magic function __call() This is ugly though it works from class A user's point of view : class B { public function method_from_b($s) { echo $s; } } class C { public function method_from_c($s) { echo $s; } } class A extends B { private $c; public function __construct() { $this->c = new C; } // fake "extends C" using magic function public function __call($method, $args) { $this->c->$method($args0); } } $a = new A; $a->method_from_b("abc"); $a->method_from_c("def") Prints "abcdef.
Answering your edit : If you really want to fake multiple inheritance, you can use the magic function __call(). This is ugly though it works from class A user's point of view : class B { public function method_from_b($s) { echo $s; } } class C { public function method_from_c($s) { echo $s; } } class A extends B { private $c; public function __construct() { $this->c = new C; } // fake "extends C" using magic function public function __call($method, $args) { $this->c->$method($args0); } } $a = new A; $a->method_from_b("abc"); $a->method_from_c("def"); Prints "abcdef.
– LOGIC9 Dec 10 '08 at 18:12 1 No limitations as far as I know, PHP is a very permissive language for little hacks like this. :) As others have pointed out, it's not the proper OOP way of doing it though. – Franck Dec 10 '08 at 18:58 magic methods cause a very small performance hit – dvb Jan 3 at 13:59 I love this idea, clean and simple!
– Max Kielland Jan 11 at 2:44.
There are plans for adding mix-ins soon, I believe. But until then, go with the accepted answer. You can abstract that out a bit to make an "extendable" class: class Extendable{ private $extender=array(); public function addExtender(Extender $obj){ $this->extenders = $obj; $obj->setExtendee($this); } public function __call($name, $params){ foreach($this->extenders as $extender){ //do reflection to see if extender has this method with this argument count if (method_exists($extender, $name)){ return call_user_func_array(array($extender, $name), $params); } } } } $foo = new Extendable(); $foo->addExtender(new OtherClass()); $foo->other_class_method(); Note that in this model "OtherClass" gets to 'know' about $foo.
OtherClass needs to have a public function called "setExtendee" to set up this relationship. Then, if it's methods are invoked from $foo, it can access $foo internally. It will not, however, get access to any private/protected methods/variables like a real extended class would.
No. You should get be and c as object inside the class a if you need function of both. If you need to have multiple inheritance by inheritance you should try to seperate the concept with Interface.
Precision: class a can inherit of class b. Class be can inherit of class c. This way a will be able to access from a function of c but you must really be sure that this as a logic between all these class.
Usually, it's better to try avoiding inheritance for no good reason.
I disagree, he can do what he asks. – George Jempty Dec 10 '08 at 14:06 class a extends be extends c? Show me that.
– Daok Dec 10 '08 at 14:06 a class cannot extends 2 class. He has to do what I said or what you said in your post. But not what he said.
– Daok Dec 10 '08 at 14:07 George, I think you misunderstood what E3 wants to do. – Franck Dec 10 '08 at 14:09 I don't think I misunderstood, he posted pseudo code, below is how you do. I'm not saying its optimum, but it does show how to do precisely what was asked.
Class c {} class be extends c {} class a extends be {} – George Jempty Dec 10 '08 at 14:13.
Classes are not meant to be just collections of methods. A class is supposed to represent an abstract concept, with both state (fields) and behaviour (methods) which changes the state. Using inheritance just to get some desired behaviour sounds like bad OO design, and exactly the reason why many languages disallow multiple inheritance: in order to prevent "spaghetti inheritance", i.e.
Extending 3 classes because each has a method you need, and ending up with a class that inherits 100 method and 20 fields, yet only ever uses 5 of them.
You cannot have a class that extends two base classes. You could not have. // this is NOT allowed (for all you google speeders) Matron extends Nurse, HumanEntity You could however have a hierarchy as follows... Matron extends Nurse Consultant extends Doctor Nurse extends HumanEntity Doctor extends HumanEntity HumanEntity extends DatabaseTable DatabaseTable extends AbstractTable and so on.
I have read several articles discouraging inheritance in projects (as opposed to libraries/frameworks), and encouraging to program agaisnt interfaces, no against implementations. They also advocate OO by composition: if you need the functions in class a and b, make c having members/fields of this type: class C { private $a, $b; public function __construct($x, $y) { $this->a = new A(42, $x); $this->b = new B($y); } protected function DoSomething() { $this->a->Act(); $this->b->Do(); } }.
You could use traits, which, hopefully, will be available from PHP 5.4. Traits is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. The semantics of the combination of Traits and classes is defined in a way, which reduces complexity and avoids the typical problems associated with multiple inheritance and Mixins.
They are recognized for their potential in supporting better composition and reuse, hence their integration in newer versions of languages such as Perl 6, Squeak, Scala, Slate and Fortress. Traits have also been ported to Java and C#. More information: https://wiki.php.
Net/rfc/traits.
Yes absolutely, just try it, in the way you describe: class be extending from c, and class a extending from b. But what you cannot do, is have class a extend both be & c.
PHP does not yet support multiple class inheritance, it does however support multiple interface inheritance. See hudzilla.org/php/6_17_0.php for some examples.
I doubt they will do it. Modern OO discourage multiple inheritance, it can be messy. – PhiLho Dec 10 '08 at 16:53.
You are asking if multiple inheritance is possible in PHP. The answer is: NO Is PHP likely to implement multiple inheritance? Someone else answered this qustion on PHPBuilder.Com with: When there's peace in Jerusalem.
Which I would interpret as : it's possible if many factors change but not very likely in the near future.
PHP does not allow multiple inheritance, but you can do with implementing multiple interfaces. If the implementation is "heavy", provide skeletal implementation for each interface in a seperate class. Then, you can delegate all interface class to these skeletal implementations via object containment.
Not knowing exactly what you're trying to achieve, I would suggest looking into the possibility of redesigning you application to use composition rather than inheritance in this case.
Always good idea is to make parent class, with functions ... i.e. Add this all functionality to parent. And "move" all classes that use this hierarchically down.
I need - rewrite functions, which are specific.
Apparently my response wasn't good enough for this message so I'll just replace it with this instead. At least I now know from "dbemerlin" below, it's not worth attempting to contribute towards anything. All well.
If I had the ability to actually "delete" this post I would.
This is not a message board. Create a new question for this. – dbemerlin Sep 7 '10 at 5:26.
You should get be and c as object inside the class a if you need function of both. If you need to have multiple inheritance by inheritance you should try to seperate the concept with Interface. Precision: class a can inherit of class b.
Class be can inherit of class c. This way a will be able to access from a function of c but you must really be sure that this as a logic between all these class. Usually, it's better to try avoiding inheritance for no good reason.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.