Req #51544 [Com]: instanceof for static calls

[email protected] ("crrodriguez at opensuse dot org")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at http://bugs.php.net/bug.php?id=51544&edit=1

 ID:               51544
 Comment by:       crrodriguez at opensuse dot org
 Reported by:      public at proside dot fr
 Summary:          instanceof for static calls
 Status:           Open
 Type:             Feature/Change Request
 Package:          Reflection related
 Operating System: Windows XP
 PHP Version:      5.3.2

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2010-04-12 22:37:39] 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();

?>


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51544&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.