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