A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearCan I extend a class using more than 1 class in PHP? - page 1
User InfoPosts
Can I extend a class using more than 1 class in PHP?#1
If I have several classes with functions that I need but want to store separately for organisation, can I extend a class to have both?

i.e. class a extends b extends c

edit: I know how to extend classes one at a time, but I'm looking for a method to instantly extend a class using multiple base classes - AFAIK you can't do this in PHP but there should be ways around it without resorting to class c extends b, class b extends a

posted date: 2008-12-10 06:02:00


Re: Can I extend a class using more than 1 class in PHP?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-10 06:02:01


Re: Can I extend a class using more than 1 class in PHP?#3
PHP does not yet support multiple class inheritance, it does however support multiple interface inheritance.See http://www.hudzilla.org/php/6_17_0.php for some examples.

posted date: 2008-12-10 06:07:00


Re: Can I extend a class using more than 1 class in PHP?#4
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, HumanEntityYou could however have a hierarchy as follows...Matron extends Nurse Consultant extends DoctorNurse extends HumanEntityDoctor extends HumanEntityHumanEntity extends DatabaseTableDatabaseTable extends AbstractTableand so on.

posted date: 2008-12-10 06:12:00


Re: Can I extend a class using more than 1 class in PHP?#5
Use aggregation or interfaces. Multiple inheritance doesn't exist in PHP.

posted date: 2008-12-10 06:19:00


Re: Can I extend a class using more than 1 class in PHP?#6
I'm looking into interfaces as I'm not a big fan of large class hierarchies. But I can't see how interfaces actually do anything?

posted date: 2008-12-10 06:32:00


Re: Can I extend a class using more than 1 class in PHP?#7
Interfaces enable you to "inherit" the API only, not function bodies. It forces class a to implement methods from interface b and c. It means that if you want to inherit behavior you must aggregate member objects of classes b and c in your class a.

posted date: 2008-12-10 06:37:00


Re: Can I extend a class using more than 1 class in PHP?#8
I mean put private $b (instance of b) and private $c (instance of c) in your class a, if that wasn't clear enough.

posted date: 2008-12-10 06:40:00


Re: Can I extend a class using more than 1 class in PHP?#9
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.

posted date: 2008-12-10 06:48:00


Re: Can I extend a class using more than 1 class in PHP?#10
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($args[0]); }}$a = new A;$a->method_from_b("abc");$a->method_from_c("def");Prints "abcdef"

posted date: 2008-12-10 07:18:00


Re: Can I extend a class using more than 1 class in PHP?#11
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.

posted date: 2008-12-10 07:21:00


Re: Can I extend a class using more than 1 class in PHP?#12
Yet? I doubt they will do it. Modern OO discourage multiple inheritance, it can be messy.

posted date: 2008-12-10 08:53:00


select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.