|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39003 __autoload unnecessarily called for type hinting
Submitted: 2006-09-30 10:05 UTC Modified: 2007-09-12 19:59 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: christoph at ziegenberg dot de Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.1.6 OS: WinXP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
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:
Solve the problem:
50 - 43 = ?
Subscribe to this entry?

 [2006-09-30 10:05 UTC] christoph at ziegenberg dot de
if i check if a variable is an instance of specific class with "instanceof" and the class i check for has not been loaded, __autoloader() is not called (as expected).

if i do the "same" check by type hinting, the __autoloader() function is called, which normally leads to including the required class file and so unnecessarily consumes memory and time.

Reproduce code:
class ClassName
    public $var = 'bla';
function test (OtherClassName $object)

function __autoload($class)
    print "try to load class $class";
$obj = new ClassName;
if (!($obj instanceof OtherClassName))
    echo "no instance of OtherClassName<br />";

Expected result:
no instance of OtherClassName

Actual result:
no instance of OtherClassName
try to load class OtherClassName
Fatal error: Class 'OtherClassName' not found in [...] on line 7


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-01 06:07 UTC] judas dot iscariote at gmail dot com
if this is the expected behaviuor ( at least, not the behaviuor I expect ;) ) it is not mentioned in the manual, so either the manual or the engine needs to be corrected ;-)
 [2006-10-01 06:51 UTC] christoph at ziegenberg dot de
My version of the manual says this (from the example code):

    // Fatal Error: Argument 1 must be an instance of OtherClass
    $foo = new stdClass;

So I should get the error "Fatal error: Argument 1 passed to test() must be an instance of OtherClassName, called in [...]", but I get the error "Fatal error: Class 'OtherClassName' not found in [...]".

Of course these are both fatal errors, but the one hand I expect another behaviour for the autoloader function as described and on the other hand the "correct" error message would help to debug the code, because the current error only refers to the __autoloader() function (so you have to use debug_backtrace()/debug_print_backtrace()).
 [2006-10-02 09:39 UTC]
Not a bug here, this is expected so marking as doc problem.
 [2006-10-02 10:42 UTC]
It's just a doc problem Tony... he's doing an instance of with an unknown class. That gives an error message before the instance of runs and can show it's error message.
 [2006-10-02 11:07 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

 [2007-09-12 19:59 UTC]
For what it worth, the expected behavior is *not* to try autoloading when checking if existing variable belongs to non-existing class, because it is obvious that existing object can not belong to non-existing class. I checked with 5.2.4, it works correctly. 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jul 14 08:01:28 2024 UTC