php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41137 is_a() for interfaces
Submitted: 2007-04-19 06:31 UTC Modified: 2007-04-29 12:53 UTC
From: amadeusz dot jasak at gmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.1 OS: -
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
25 + 9 = ?
Subscribe to this entry?

 
 [2007-04-19 06:31 UTC] amadeusz dot jasak at gmail dot com
Description:
------------
is_a function will be works if first argument is class name and next argument is interface name. Currently it isn't working... I can't check for class (not object) is implementing some interface

Reproduce code:
---------------
<?php

inteface factory
{
  public static function _factory($params);
}

class foo implements factory
{
  public static function _factory($params)
  {
    $t = new foo();
    return $t; // Sweet use of factory ;)
  }
}

var_dump
(
  is_a('foo', 'factory'), // Currently return false, but true is expected
  is_a($foo = new foo(), 'factory') // It is working but it isn't practicly
);
?>

Expected result:
----------------
bool(true)
bool(true)

Actual result:
--------------
bool(false)
bool(true)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-19 15:01 UTC] cellog@php.net
quoting the manual (http://php.net/is_a):

"bool is_a ( object $object, string $class_name )

Checks if the given object is of this class or has this class as one 
of its parents."

'foo' is not an object (it is a string), and therefore is_a() 
returns false.

is_a('foo', 'foo')

is also false for the same reason.
 [2007-04-19 15:18 UTC] amadeusz dot jasak at gmail dot com
So, maybe new function for checking interface implementation for classes?? Like bool is_implementing(mixed $class_or_object, string $interface)
 [2007-04-29 12:38 UTC] stas@php.net
I think reflection extension allows to do such checks.
 [2007-04-29 12:48 UTC] amadeusz dot jasak at gmail dot com
Yes, I know, but not everyone has Reflection API, reflection slow...
 [2007-04-29 12:53 UTC] stas@php.net
I don't think reflection is really much slower than is_a - it adds a couple of object creations, but that's it. Not sure what you mean by "not everyone has" - what could be the reason not to have it?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC