note 96137 added to language.oop5.interfaces
| From: | uramihsayibok at osu1 dot php dot net | Date: | Wed, 10 Feb 2010 19:25:52 +0000 |
| Subject: | note 96137 added to language.oop5.interfaces | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165838@lists.php.net to get a copy of this message | ||
Interfaces can define static methods, but note that this won't make sense as you'll be
using the class name and not polymorphism.
...Unless you have PHP 5.3 which supports late static binding:
<?php
interface IDoSomething {
public static function doSomething();
}
class One implements IDoSomething {
public static function doSomething() {
echo "One is doing something\n";
}
}
class Two extends One {
public static function doSomething() {
echo "Two is doing something\n";
}
}
function example(IDoSomething $doer) {
$doer::doSomething(); // "unexpected ::" in PHP 5.2
}
example(new One()); // One is doing something
example(new Two()); // Two is doing something
?>
If you have PHP 5.2 you can still declare static methods in interfaces. While you won't be able
to call them via LSB, the "implements IDoSomething" can serve as a hint/reminder to other
developers by saying "this class has a ::doSomething() method".
Besides, you'll be upgrading to 5.3 soon, right? Right?
(Heh. I just realized: "I do something". Unintentional, I swear!)
----
Server IP: 64.71.164.2
Probable Submitter: 69.166.47.131
----
Manual Page -- http://www.php.net/manual/en/language.oop5.interfaces.php
Edit -- https://master.php.net/note/edit/96137
Del: integrated -- https://master.php.net/note/delete/96137/integrated
Del: useless -- https://master.php.net/note/delete/96137/useless
Del: bad code -- https://master.php.net/note/delete/96137/bad+code
Del: spam -- https://master.php.net/note/delete/96137/spam
Del: non-english -- https://master.php.net/note/delete/96137/non-english
Del: in docs -- https://master.php.net/note/delete/96137/in+docs
Del: other reasons-- https://master.php.net/note/delete/96137
Reject -- https://master.php.net/note/reject/96137
Search -- https://master.php.net/manage/user-notes.php