|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-04-12 22:37 UTC] public at proside dot fr
Description:
------------
Hy guys,
Here's a problem i faced today : i wanted to know if a base and derived class having only static functions and using both late static binding were an implementation of a specific interface.
The problem was that the class base needed to know if its child implemented a specific interface.
Not easy to explain, the code is easier for a better comprehension :
Test script:
---------------
<?php
interface iTest { }
class base {
public static function create() {
if (self instanceof iTest) echo 'iTest';
}
}
class child extends base implements iTest { }
child::create();
?>
Expected result:
----------------
Normally the result should be 'iTest' because the class 'child' implements iTest
Actual result:
--------------
Doesn't execute.
Here's the solution i found : i used the ReflectionCLass to emulate that feature
<?php
interface iTest { }
class base {
public static function create() {
$r = new ReflectionClass(static::$SELF);
if (in_array('iTest', $r->getInterfaceNames())) echo 'iTest';
}
}
class child extends base implements iTest {
protected static $SELF = __CLASS__;
}
child::create();
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 07:00:01 2025 UTC |
<?php interface iTest { } class base { public static function create() { if (class_implements(get_called_class(),'iTest')) echo 'iTest'; } } class child extends base implements iTest { } child::create(); Fixes your problem, this is not a PHP bug.