php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38504 class_exists strange behaviour
Submitted: 2006-08-18 18:27 UTC Modified: 2006-08-21 08:26 UTC
From: kn1g at yahoo dot com dot br Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.4.4 OS: Linux
Private report: No CVE-ID: None
 [2006-08-18 18:27 UTC] kn1g at yahoo dot com dot br
Description:
------------
class_exists with return appears to avoid setting a variable after a class definition.

Avoiding the return  inside the if block like work as expected:
<?
if(!class_exists('newclass')){
 class newclass(){
 }
}
$var_newclass = "newclass ok\n";
?>

But I couldn't find anything in the documentation explaining this behaviour.

Reproduce code:
---------------
"class.php"
<?
if(class_exists('newclass')){
    return;
}
class newclass{
    function newclass(){
        echo "hi\n";
    }
}
$var_newclass = "newclass ok\n";
?>

"test.php"
<?
include("class.php");
new newclass;
echo $var_newclass;
?>

Expected result:
----------------
hi
newclass ok


Actual result:
--------------
hi

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-18 20:38 UTC] tony2001@php.net
Classes are declared during the compile time.
So the class DOES exist and when you actually run the code, the if condition evaluates to TRUE, hence return is executed.
This is expected behaviour.
 [2006-08-19 02:45 UTC] kn1g at yahoo dot com dot br
I see, well, I think that class_exists function manual could have a note warning about this (I didn't find this information anywhere).
 [2006-08-21 08:26 UTC] tony2001@php.net
You can always add a note youself.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 19:01:32 2024 UTC