Req #51544 [Opn->Bgs]: instanceof for static calls

From: Date: Thu, 27 May 2010 23:27:29 +0000
Subject: Req #51544 [Opn->Bgs]: instanceof for static calls
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-150894@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51544&edit=1

 ID:               51544
 Updated by:       kalle@php.net
 Reported by:      public at proside dot fr
 Summary:          instanceof for static calls
-Status:           Open
+Status:           Bogus
 Type:             Feature/Change Request
 Package:          Reflection related
 Operating System: Windows XP
 PHP Version:      5.3.2

 New Comment:

.


Previous Comments:
------------------------------------------------------------------------
[2010-04-16 03:08:15] crrodriguez at opensuse dot org

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

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


Thread (3 messages)

« previous php.bugs (#150894) next »