php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64036 class_exists behavior while compile
Submitted: 2013-01-21 13:11 UTC Modified: 2013-01-22 00:28 UTC
From: dirk dot gerigk at atraveo dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2013-01-21 13:11 UTC] dirk dot gerigk at atraveo dot de
Description:
------------
PHP Version 5.3.9-ZS5.6.0

Is it an Complie Error or is it wanted in that way how it works!
Please see the "Test Script" & "Actual Result".

Only way to go around it (Force negative class_exists check and braces ):
if(!class_exists('test')){
  class test {}   
} 

For me it seems to be that the Compiler complies the hole file and declare directly the given class in the file. 
Only doesnt declare if the class defenition is within a function.
But why can i change the result via    'test' | $t='test' ??
And see "Actual Result" as second question.
 

Test script:
---------------
//fileA.php - test is not included yet!!
if(class_exists('test')) die('complier has included test');
class test {}
//wont work - doesnt include the test class

//fileB.php - test is not included yet!!
if(class_exists($t='test')) die('wont never happen');
class test {}
//works - includes test class

Note: if u try that code with eval() both cases wont work! 
eval("\$t='test'; if(class_exists(\$t)){ die('complier has included test'); } class test {}");



Expected result:
----------------
In thing it is no really cool to always force "class definitions" 
in an negative "class_exists" check and then drop the class code within the {}.
And i cannot really behind the diff. behavior on the eval() tryout.
Please Note the add in "Actual Result". 
  

Actual result:
--------------
//see "Test Script" and here is an crazy tryout on this issue:
error_reporting(E_ALL); 
$t='test'; if(class_exists($t,false)){ echo ('complier has included '.$t);} 
class test {public $var='1';} 
class test {public $var='2';}

What did you expect?
Compile Error before running any Script??? 
So this: 
Fatal error: Cannot redeclare class test in C:\xampp\htdocs\js\in.php on line 5
Or this:
complier has included test
Fatal error: Cannot redeclare class test in C:\xampp\htdocs\js\in.php on line 5

Test it! 
I thing this behavior is a little bit wired. Maybe only for me ;)

Thanks for the time.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-22 00:28 UTC] aharvey@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Your reproduction case isn't really clear, but this seems to work as expected for me.
 [2013-01-22 00:28 UTC] aharvey@php.net
-Status: Open +Status: Not a bug -Package: *Compile Issues +Package: Scripting Engine problem
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Aug 02 14:00:02 2025 UTC