php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28025 function redeclared when return before its declaration
Submitted: 2004-04-16 11:04 UTC Modified: 2005-04-27 12:06 UTC
Votes:10
Avg. Score:4.2 ± 0.9
Reproduced:8 of 8 (100.0%)
Same Version:4 (50.0%)
Same OS:5 (62.5%)
From: Christian dot Lefebvre at atosorigin dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5CVS-2005-01-10 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Christian dot Lefebvre at atosorigin dot com
New email:
PHP Version: OS:

 

 [2004-04-16 11:04 UTC] Christian dot Lefebvre at atosorigin dot com
Description:
------------
lot of php3 codes begin include files by a
  if(defined(..)) return; define(..)
to simulate include_once behavior

it works fine with php4, but with php5, functions
declared after this return raise "cannot redeclare" error
when the file is included twice.


Reproduce code:
---------------
t1.php :
<?php
include("t2.php");
toto();
include("t2.php");
toto();
?>

t2.php :
<?php
error_log("in t2");

if (defined("_T2_INCLUDED")) {
  error_log("already went there");
  return;
}
define("_T2_INCLUDED", "1");

error_log("still in t2");

function toto() {
  echo "Here am I !!\n";
}
?>


Expected result:
----------------
with php4 :
in t2
still in t2
Here am I !!
in t2
already went there
Here am I !!


Actual result:
--------------
with php5 :
in t2
still in t2
Here am I !!

Fatal error: Cannot redeclare toto() (previously declared in t2.php:13) in t2.php on line 14


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-10 16:49 UTC] patrice at lazareff dot com
Same behavior if exit is used instead of return, i.e:

if(defined("MY_CONSTANT")) exit;

define("MY_CONSTANT", "1");

function foo() {}

The only workaround seems to be using an else statement:

if(defined("MY_CONSTANT") return;

else {
    function foo() {}
}

how sad...
 [2005-04-27 12:06 UTC] zeev@php.net
Unfortunately this bug cannot be fixed with the new architecture of PHP 5.  The workaround you specified is the right solution to this problem.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 19:01:28 2024 UTC