php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55000 instanceof should fail compile-time if checking for non existant class
Submitted: 2011-06-06 16:13 UTC Modified: 2011-06-07 21:00 UTC
From: lenar at city dot ee Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
 [2011-06-06 16:13 UTC] lenar at city dot ee
Description:
------------
If the use clause is missing (class/interface not found in current namespace) trigger some fatal error.

This would catch many errors when the use clause
is just forgotten by developer.

I would understand the current behavior for those cases:

1) $a instanceof $b
2) $a instanceof 'BarClass' (not supported currently afaik)



Test script:
---------------
<?php

namespace Test;

// use FooNamespace\BarClass;

$a = new \stdClass();
var_dump($a instanceof BarClass);

Expected result:
----------------
Fatal error or at least some Exception.

Actual result:
--------------
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-07 20:33 UTC] dsp@php.net
-Status: Open +Status: Bogus
 [2011-06-07 20:33 UTC] dsp@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. You can do something like

<?php
namespace A;

include_once('partial-namespace-a.php');

var_dump($a instanceof Test);

which can evaluate to true or false depending on the included file.
 [2011-06-07 21:00 UTC] lenar at city dot ee
Thank you for answering.

But I think this is really a bug. Maybe it's different mindsets but I really think

$a instanceof NonExistentClass

should fail as would

$a = new NonExistentClass()

Sorry, I don't see the reasoning behind 'this is expected behavior'. The
current behavior seems to be some evolutionary mishap at most, not something
an average developer would expect from instanceof operator as it easily provides
a way to shoot oneself in a leg (think simple typo). It's not really KISS.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 07:01:28 2024 UTC