php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55246 instanceof does not complain even if class is never defined
Submitted: 2011-07-20 04:31 UTC Modified: 2011-07-20 05:01 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: x11 at arturaz dot net Assigned:
Status: Not a bug Package: Compile Failure
PHP Version: 5.3.6 OS: Ubuntu Linux
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: x11 at arturaz dot net
New email:
PHP Version: OS:

 

 [2011-07-20 04:31 UTC] x11 at arturaz dot net
Description:
------------
PHP does not complain if you check for non-existant class with instanceof.

This provides space for subtle errors, like checking for DateRange when only Date_Range class is defined.

Test script:
---------------
<?php
echo PHP_VERSION . "\n";
error_reporting(E_ALL);
class Foo {}
$a = new Foo;
// Notice that Bar class is never defined, however PHP does not complain about missing class.
if ($a instanceof Bar) { echo "Should be an error\n"; }
else { echo "PHP fail\n"; }

Expected result:
----------------
Compiler error thrown saying that Bar was not found.

Actual result:
--------------
Nothing happens.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-20 04:33 UTC] pajoye@php.net
-Status: Open +Status: Bogus
 [2011-07-20 04:33 UTC] pajoye@php.net
That's the exact idea of instanceof to test something without getting noises.
 [2011-07-20 04:34 UTC] x11 at arturaz dot net
How it's possibly useful to test if object is an instance of class which is not even defined?
 [2011-07-20 04:55 UTC] pajoye@php.net
Think of autoloader, or other related tests. A warning or error here is not 
welcome.
 [2011-07-20 05:01 UTC] x11 at arturaz dot net
Yeah, autoloader should try to load the class and then, if unsuccessful - fail.

Seriously. I know that it's cool to have funny glitches in PHP like FOO reverting to 'FOO' if it's undefined, but this?
 [2014-07-04 09:02 UTC] bugs dot php dot net at chsc dot dk
It would be useful with a switch that makes this trigger a notice/warning in non-production environments, though.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 14:01:31 2025 UTC