Liquid Fire Posted January 19, 2008 Share Posted January 19, 2008 Is multiple inheritance possible in php? for the time being I am doing this: class a { } class b extends a { } class c extends b { } Which does provide the same effect but it be nicer if I would do class a { } class b { } class c extends a, b { } Quote Link to comment https://forums.phpfreaks.com/topic/86819-solved-multiple-inheritance/ Share on other sites More sharing options...
trq Posted January 19, 2008 Share Posted January 19, 2008 Is multiple inheritance possible in php? No. Quote Link to comment https://forums.phpfreaks.com/topic/86819-solved-multiple-inheritance/#findComment-443801 Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 You can think of it a bit like taxonomic ranks in biological classification. E.g. there can be multiple families under a specific class, but a family can only belong to one class. Quote Link to comment https://forums.phpfreaks.com/topic/86819-solved-multiple-inheritance/#findComment-443834 Share on other sites More sharing options...
deadimp Posted January 23, 2008 Share Posted January 23, 2008 Ran into the same problem: http://www.phpfreaks.com/forums/index.php/topic,152955.0.html However, if you're into interfaces, you can have one class implement multiple interfaces. interface Honest { function etc(); } interface Rock { function stuff(); } class Frankenstein implements Honest, Rock { function etc() { echo "blerg\n"; } function stuff() { echo "blarg\n"; } } $obj=new A; $obj->etc(); $obj->stuff(); Of course, you can't put any meat into your interface method declarations... Quote Link to comment https://forums.phpfreaks.com/topic/86819-solved-multiple-inheritance/#findComment-446535 Share on other sites More sharing options...
Daniel0 Posted January 23, 2008 Share Posted January 23, 2008 That would be incorrect usage of a interfaces. Interfaces are used to, well... implement interfaces for the class. It's used to ensure that an object implements certain functionality. E.g. you may have an interface called XMLable. You can then check if the object is a valid object for your usage (i.e. if it contains functionality to return XML data for the object) like this: <?php if( !($object instanceof XMLable ) ) { throw Exception('Invalid object. Object must implemennt XMLable'); } // OR class SomeClass { // ... public function doSomething( XMLable $object ) { // do something in here with $object which is ensured to have implemented XMLable } // ... } ?> In the latter example, if $object is not an object which implements XMLable then it will result in a catchable fatal error. Quote Link to comment https://forums.phpfreaks.com/topic/86819-solved-multiple-inheritance/#findComment-446709 Share on other sites More sharing options...
andygrant Posted September 23, 2008 Share Posted September 23, 2008 Hi I have this same issue and was wondering if someone could give me some advice on multiple extends, my situation is below: Class A: Finished object that doesn't extend anything Class B: Finished object that doesn't extend anything Class C: Not developed but needs to inherit everything from A and B Normally I have things extend A or B but I now need a new object to extend both, therefore I was wondering is that possible to achieve this without altering A and/or B so that one of them extends the other before I can create C? Ideally I would like to keep A and B unchanged as they are standard objects I use for various things but I haven't needed them together before. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/86819-solved-multiple-inheritance/#findComment-649107 Share on other sites More sharing options...
Daniel0 Posted September 23, 2008 Share Posted September 23, 2008 You can't. PHP does not have multiple inheritance. Quote Link to comment https://forums.phpfreaks.com/topic/86819-solved-multiple-inheritance/#findComment-649126 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.