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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: public at proside dot fr
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2019 The PHP Group
All rights reserved.
Last updated: Wed Mar 27 01:01:27 2019 UTC