php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51544 instanceof for static calls
Submitted: 2010-04-12 22:37 UTC Modified: 2010-05-28 01:27 UTC
From: public at proside dot fr Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 5.3.2 OS: Windows XP
Private report: No CVE-ID: None
 [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();

?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-16 03:08 UTC] 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-05-28 01:27 UTC] kalle@php.net
-Status: Open +Status: Bogus
 [2010-05-28 01:27 UTC] kalle@php.net
.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 19:01:28 2024 UTC