php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53727 Inconsistent behavior of is_subclass_of with interfaces
Submitted: 2011-01-12 17:08 UTC Modified: 2011-07-04 11:00 UTC
Votes:3
Avg. Score:3.3 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: mattknight at xymail dot co dot uk Assigned: dmitry (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.3.5 OS: Gentoo x86_64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mattknight at xymail dot co dot uk
New email:
PHP Version: OS:

 

 [2011-01-12 17:08 UTC] mattknight at xymail dot co dot uk
Description:
------------
is_subclass_of() can take a string as the first parameter, as well as a second string parameter of the class name. It will check to see whether a class with the name specified in the first parameter is a subclass of the class with the name specified in the second parameter.

This logic, however, doesn't apply with interfaces.

The function behaves as expected for classes that don't directly implement an interface, but won't recognise the interface on a class that does directly implement it.

Test script:
---------------
<?php

interface MyInterface {
    const TEST_CONSTANT = true;
}

class ParentClass implements MyInterface { }

class ChildClass extends ParentClass { }

echo (is_subclass_of('ChildClass', 'MyInterface') ? 'true' : 'false') . "\n";
echo (defined('ChildClass::TEST_CONSTANT') ? 'true' : 'false') . "\n";

echo (is_subclass_of('ParentClass', 'MyInterface') ? 'true' : 'false') . "\n";
echo (defined('ParentClass::TEST_CONSTANT') ? 'true' : 'false') . "\n";

Expected result:
----------------
true
true
true
true

Actual result:
--------------
true
true
false
true

Patches

fix-is_subclass_of-PHP_5_3.diff (last revision 2011-06-30 22:33 UTC by ralph at smashlabs dot com)
is_subclass_of-ignoring_interfaces (last revision 2011-01-13 00:06 UTC by marc at easen dot co dot uk)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-12 17:58 UTC] chris at cmbuckley dot co dot uk
One could argue that the expected result is actually:

true
true
false
false

since neither classes are technically "subclasses" of the interface. Either way, the actual behaviour is undesirable :-)
 [2011-01-12 18:06 UTC] chris at cmbuckley dot co dot uk
Apologies; I actually meant:

false
true
false
true
 [2011-01-13 09:13 UTC] mattknight at xymail dot co dot uk
Yes, it would also be fine to return false when comparing against an ancestral interface, just so long as the behaviour is consistent.

Expected result:
----------------
false
true
false
true
 [2011-06-30 18:45 UTC] ralph at smashlabs dot com
Dmitry,

The patch called fix-is_subclass_of-PHP_5_3.diff does the following:

* Alters the existing Zend/tests/is_a.phpt to alter the bad
  expectation,

* Adds a new test file for is_subclass_of() as 
  Zend/tests/is_subclass_of.phpt

* Finally alters is_a_impl() in the following ways:

  * If an object was provided as the first parameter, and we are
    inside of an is_subclass_of call (known by the flag
    only_subclass), then assign the class entry of the object

  * Next, determine if the instance_ce provided is the same class
    entry as that we are testing against (parameter 2 of the
    is_subclass_of() call ... if it is, return false

  * Now that we know the instance_ce is not the same as the class
    entry for the test class, allow is_a_impl to pass these class
    entries to instanceof_function, and return that value

It is pretty straight forward, and any way I look at it, there are
few consequences to this patch.

Thanks,
Ralph
 [2011-07-01 02:54 UTC] dmitry@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: dmitry
 [2011-07-04 10:55 UTC] dmitry@php.net
Automatic comment from SVN on behalf of dmitry
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=312904
Log: Fixed bug #53727 (Inconsistent behavior of is_subclass_of with interfaces)
 [2011-07-04 11:00 UTC] dmitry@php.net
-Status: Assigned +Status: Closed
 [2011-07-04 11:00 UTC] dmitry@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2011-07-15 15:17 UTC] philip@php.net
Automatic comment from SVN on behalf of philip
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=313271
Log: Updated is_a() and is_subclass_of() tests as per the removed E_WARNING from r313162 which was inspired by discussion after r312904 and PHP Bug #53727
 [2011-09-02 11:38 UTC] jbondc at openmv dot com
This patch also modifies the is_a() api by allowing a string as a first argument.

That seems wrong to me within the 5.3 branch.
 [2012-04-18 09:50 UTC] laruence@php.net
Automatic comment on behalf of dmitry
Revision: http://git.php.net/?p=php-src.git;a=commit;h=34b8924f958e06bcfdd3a8c0719e1a2a4edbd506
Log: Fixed bug #53727 (Inconsistent behavior of is_subclass_of with interfaces)
 [2012-07-24 23:41 UTC] rasmus@php.net
Automatic comment on behalf of dmitry
Revision: http://git.php.net/?p=php-src.git;a=commit;h=34b8924f958e06bcfdd3a8c0719e1a2a4edbd506
Log: Fixed bug #53727 (Inconsistent behavior of is_subclass_of with interfaces)
 [2013-11-17 09:37 UTC] laruence@php.net
Automatic comment on behalf of dmitry
Revision: http://git.php.net/?p=php-src.git;a=commit;h=34b8924f958e06bcfdd3a8c0719e1a2a4edbd506
Log: Fixed bug #53727 (Inconsistent behavior of is_subclass_of with interfaces)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC