Jump to content

[SOLVED] Multiple Inheritance?


Liquid Fire

Recommended Posts

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...

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.

  • 8 months later...

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.

 

 

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.