php.net |  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
Votes:1
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:
 [2006-09-30 10:05 UTC] christoph at ziegenberg dot de
Description:
------------
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:
---------------
<?php
error_reporting(E_ALL);
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 />";
}
test($obj);
?>

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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;
    $myclass->test($foo);

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] derick@php.net
Not a bug here, this is expected so marking as doc problem.
 [2006-10-02 10:42 UTC] derick@php.net
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] tony2001@php.net
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] stas@php.net
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-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 19:01:51 2014 UTC