This is a discussion on interface inheritance within the PHP General forums, part of the PHP Programming Forums category; all, previously, on this list and elsewhere, i have raised the topic of interface inheritance, lamenting that php is void ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
all,
previously, on this list and elsewhere, i have raised the topic of interface inheritance, lamenting that php is void of the feature. to my utter amazement i discovered that php does in fact support interface inheritance, reading through some of Marcus Boergers' code in SPL.. i noticed RecursiveIterator extends Iterator, which is an internal interface (part of the zend engine), so i thought to myself, perhaps only internal interfaces can be extended, but then a quick test proved my suspicions wrong. in fact, interfaces can even extend multiple parent interfaces, just like java (*ducks*). here is the sample code so you can see for yourself: <?php interface Somethingable { public function doSomething(); } interface Crazyfiable { public function getCrazy(); } interface Extendable extends Somethingable, Crazyfiable { public function extend(); } class SuperClass implements Extendable { public function doSomething() { echo 'i did something ..' . PHP_EOL; } public function extend() { echo 'i extended something..' . PHP_EOL; } public function getCrazy() { echo 'im a crazy bastard now!' . PHP_EOL; } } /* * TEST HERE */ $sc = new SuperClass(); $sc->doSomething(); $sc->extend(); $sc->getCrazy(); ?> and the output: nathan@trident /usr/share/php5/splExperiment/tests $ php testInterfaceInheritence.php i did something .. i extended something.. im a crazy bastard now! i for one am quite excited. heres to happy adventures in oop-land w/ php! -nathan |
|
|||
|
Nathan Nobbe schreef:
> all, > > previously, on this list and elsewhere, i have raised the topic of > interface inheritance, lamenting that php is void of the feature. > to my utter amazement i discovered that php does in fact support > interface inheritance, reading through some of Marcus Boergers' > code in SPL.. i noticed RecursiveIterator extends Iterator, which > is an internal interface (part of the zend engine), so i thought to > myself, perhaps only internal interfaces can be extended, but > then a quick test proved my suspicions wrong. in fact, interfaces > can even extend multiple parent interfaces, just like java (*ducks*). > here is the sample code so you can see for yourself: > > <?php > interface Somethingable { > public function doSomething(); > } > > interface Crazyfiable { > public function getCrazy(); > } > > interface Extendable extends Somethingable, Crazyfiable { > public function extend(); > } > > class SuperClass implements Extendable { > public function doSomething() { > echo 'i did something ..' . PHP_EOL; > } > > public function extend() { > echo 'i extended something..' . PHP_EOL; > } > > public function getCrazy() { > echo 'im a crazy bastard now!' . PHP_EOL; > } > } > > /* > * TEST HERE > */ > $sc = new SuperClass(); > $sc->doSomething(); > $sc->extend(); > $sc->getCrazy(); > ?> > > and the output: > nathan@trident /usr/share/php5/splExperiment/tests $ php > testInterfaceInheritence.php > i did something .. > i extended something.. > im a crazy bastard now! > > i for one am quite excited. heres to happy adventures in oop-land w/ php! nice bit of info! as an aside, I often use php -r to test snippets like this e.g.: $> php -r 'echo "foo!";' very handy and saves having to create a file everytime you test something, it does mean your forced to use double quotes only in the code (otherwise the shell misinterprets). I recommend using double quotes in snippets you post in order to make it easier for people to quickly test your gems for themselves :-) > > -nathan > |