note 96368 added to language.oop5.interfaces
| From: | drieick at hotmail dot com | Date: | Tue, 23 Feb 2010 17:29:28 +0000 |
| Subject: | note 96368 added to language.oop5.interfaces | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166291@lists.php.net to get a copy of this message | ||
I was wondering if implementing interfaces will take into account inheritance. That is, can
inherited methods be used to follow an interface's structure?
<?php
interface Auxiliary_Platform {
public function Weapon();
public function Health();
public function Shields();
}
class T805 implements Auxiliary_Platform {
public function Weapon() {
var_dump(__CLASS__);
}
public function Health() {
var_dump(__CLASS__ . "::" . __FUNCTION__);
}
public function Shields() {
var_dump(__CLASS__ . "->" . __FUNCTION__);
}
}
class T806 extends T805 implements Auxiliary_Platform {
public function Weapon() {
var_dump(__CLASS__);
}
public function Shields() {
var_dump(__CLASS__ . "->" . __FUNCTION__);
}
}
$T805 = new T805();
$T805->Weapon();
$T805->Health();
$T805->Shields();
echo "<hr />";
$T806 = new T806();
$T806->Weapon();
$T806->Health();
$T806->Shields();
/* Output:
string(4) "T805"
string(12) "T805::Health"
string(13) "T805->Shields"
<hr />string(4) "T806"
string(12) "T805::Health"
string(13) "T806->Shields"
*/
?>
Class T805 implements the interface Auxiliary_Platform. T806 does the same thing, but the method
Health() is inherited from T805 (not the exact case, but you get the idea). PHP seems to be fine
with this and everything still works fine. Do note that the rules for class inheritance doesn't
change in this scenario.
If the code were to be the same, but instead T805 (or T806) DOES NOT implement Auxiliary_Platform,
then it'll still work. Since T805 already follows the interface, everything that inherits T805
will also be valid. I would be careful about that. Personally, I don't consider this a bug.
This seems to work in PHP5.2.9-2, PHP5.3 and PHP5.3.1 (my current versions).
We could also do the opposite:
<?php
class T805 {
public function Weapon() {
var_dump(__CLASS__);
}
}
class T806 extends T805 implements Auxiliary_Platform {
public function Health() {
var_dump(__CLASS__ . "::" . __FUNCTION__);
}
public function Shields() {
var_dump(__CLASS__ . "->" . __FUNCTION__);
}
}
$T805 = new T805();
$T805->Weapon();
echo "<hr />";
$T806 = new T806();
$T806->Weapon();
$T806->Health();
$T806->Shields();
/* Output:
string(4) "T805"
<hr />string(4) "T805"
string(12) "T806::Health"
string(13) "T806->Shields"
*/
?>
This works as well, but the output is different. I'd be careful with this.
----
Server IP: 192.41.40.227
Probable Submitter: 71.22.33.6
----
Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php
Edit -- https://master.php.net/note/edit/96368
Del: integrated -- https://master.php.net/note/delete/96368/integrated
Del: useless -- https://master.php.net/note/delete/96368/useless
Del: bad code -- https://master.php.net/note/delete/96368/bad+code
Del: spam -- https://master.php.net/note/delete/96368/spam
Del: non-english -- https://master.php.net/note/delete/96368/non-english
Del: in docs -- https://master.php.net/note/delete/96368/in+docs
Del: other reasons-- https://master.php.net/note/delete/96368
Reject -- https://master.php.net/note/reject/96368
Search -- https://master.php.net/manage/user-notes.php