Bluehost.com Web Hosting $6.95

interface inheritance

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


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-28-2008
Nathan Nobbe
 
Posts: n/a
Default interface inheritance

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

Reply With Quote
  #2 (permalink)  
Old 01-28-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] interface inheritance

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
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 04:58 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0