php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61526 is_subclass_of(string, string, false) always returns false
Submitted: 2012-03-27 09:46 UTC Modified: 2012-03-31 08:29 UTC
From: gtisza at gmail dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.3.10 OS: Debian
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: gtisza at gmail dot com
New email:
PHP Version: OS:

 

 [2012-03-27 09:46 UTC] gtisza at gmail dot com
Description:
------------
According to the documentation ( http://php.net/manual/en/function.is-subclass-of.php ) the third parameter of is_subclass_of decides "Whether to call autoloader if the class doesn't exist." 

That means that is_subclass_of('class1', 'class2', false) should
- return true if class1 is loaded and a subclass of class2
- return false if class1 is loaded but not a subclass of class2
- return false if class1 is not loaded

What actually happens is that it always returns false, no matter what. The naming of the parameter - "$allow_string" - suggests this is somewhat intentional, but it is a completely unintuitive and unhelpful behavior, which makes the option completely useless: is_subclass_of with $allow_string set to false becomes worthless if the first argument is a string, and if it is an object, the third parameter is pointless again as the class must have been loaded already.

Test script:
---------------
class Super {}
class Sub extends Super {}

var_dump(is_subclass_of('Sub', 'Super')); // true

var_dump(is_subclass_of('Sub', 'Super'), false); // should be true but false



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-29 02:37 UTC] yohgaki@php.net
The same behavior in PHP 5.4.1RC.

Is this a doc problem or bug in PHP?

From http://php.net/manual/en/function.is-subclass-of.php

----
allow_string
Whether to call autoloader if the class doesn't exist.
----

----
5.3.9	 Added allow_string parameter
5.0.3	 You may also specify the object parameter as a string (the name of the 
class)
----

I'm not sure, but is this a bug introduced by is_a() fix?
I didn't follow the discussion, so I hope someone could address this issue. 

BTW, correct PoC would be following code.

<?php
class Super {}
class Sub extends Super {}

var_dump(is_subclass_of('Sub', 'Super')); // true
var_dump(is_subclass_of('Sub', 'Super', true)); // true
var_dump(is_subclass_of('Sub', 'Super', false)); // false
?>
 [2012-03-31 08:04 UTC] stas@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected behavior. Please see bug #55475 for more info.
 [2012-03-31 08:04 UTC] stas@php.net
-Status: Open +Status: Not a bug
 [2012-03-31 08:29 UTC] yohgaki@php.net
Automatic comment from SVN on behalf of yohgaki
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=324657
Log: Fix #61526 Improve doc.
 [2012-03-31 08:29 UTC] yohgaki@php.net
I committed doc improvement.
 [2012-03-31 09:48 UTC] yohgaki@php.net
Automatic comment from SVN on behalf of yohgaki
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=324663
Log: Fix #61526 Improve doc.
 [2020-02-07 06:08 UTC] phpdocbot@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=321e62884a4c0cc708517c428ba2232e9a6c7227
Log: Fix #61526 Improve doc.
 [2020-02-07 06:08 UTC] phpdocbot@php.net
-Status: Not a bug +Status: Closed
 [2020-02-07 06:08 UTC] phpdocbot@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=ee0afeeaed7494107522591b00d2d445ccd84e7d
Log: Fix #61526 Improve doc.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 03:01:28 2025 UTC