php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44021 is_subclass_of Does Not Accept Namespaces
Submitted: 2008-02-02 02:01 UTC Modified: 2008-02-02 05:47 UTC
From: rpanning at hotmail dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.3CVS-2008-02-02 (snap) OS: Windows XP SP2
Private report: No CVE-ID: None
 [2008-02-02 02:01 UTC] rpanning at hotmail dot com
Description:
------------
The is_subclass_of() function does not accept classes in namespaces. A warning is generated and the statement results as FALSE.

Also, in the example, if the namespace definition is removed it works.

Reproduce code:
---------------
namespace NS;

class ParentClass {}
class ChildClass extends ParentClass {}

if (is_subclass_of('ChildClass', 'ParentClass')) {
	print 'Yes';
} else {
	print 'No';
}


Expected result:
----------------
Yes

Actual result:
--------------
Warning: Unknown class passed as parameter in C:\test.php on line 7
No

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-02 03:35 UTC] cellog@php.net
<?php
namespace NS;

class ParentClass {}
class ChildClass extends ParentClass {}

if (is_subclass_of('NS::ChildClass', 'NS::ParentClass')) {
	print 'Yes';
} else {
	print 'No';
}
?>

Does the above code work for you?  If yes, please close this bug as bogus.

Automatic prefixing only occurs with absolute classnames, not compiled variables.  Imagine, for instance this code:

<?php
namespace Foo;
function testsit($cl)
{
    return is_subclass_of($cl, 'ParentClass');
}
?>

Now, if one calls this code from this file:

<?php
namespace Blah;
var_dump(testsit('ChildClass'));
?>

should it look for Blah::ChildClass, Foo::ChildClass, or ::ChildClass?  There is no deterministic way to answer this question.  Therefore fully qualified classnames must be used for anything that isn't a T_STRING.
 [2008-02-02 05:47 UTC] rpanning at hotmail dot com
Alright, that does work and I did further testing and it seems to work. Unfortunately I deleted my original tests so I cannot determine what I was originally trying. Thanks
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 14:01:29 2024 UTC