|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
Patchesfix-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 RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 15:00:01 2025 UTC |
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