php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #26032 return in included files as not always the same behaviour
Submitted: 2003-10-29 17:16 UTC Modified: 2004-07-27 23:14 UTC
From: kevin at hatry dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.2 OS: irrelevant
Private report: No CVE-ID: None
 [2003-10-29 17:16 UTC] kevin at hatry dot com
Description:
------------
First i'll describe the more or less documented way of doing:

- When putting a return in an included file, the execution of the file stops and returns to the parent file (all normal), but functions defined in the included file are nontheless put in the environnement (they are callable in the parent file). OK i understand that: functions are added in the environnement at parsing time (so before execution time).

- When including two times the same file which defines a function, i get a fatal error saying the function is already defined. (which is normal).

But when you mix both, ie: an included file defining a function with a return at the beginning and that you include that file two times you dont have the fatal error: php seems to ignore the second declarations.

Of course the example is trivial but it wotks the same if the includes are done in separate files.

A last remark: if you add code after the return, it will never be executed (that's normal) whereas the function will exists which is disturbing : i spent half an hour wondering why my $GLOBALS affectation put after the return (which was in an if statement) didnt appear even when i could use the function !

Even if it's not considered as a bug i think it should be properly documented.

Thanks.

Reproduce code:
---------------
<?php
// this is the included file: "inc.php" **************
return;

function dummy () { return 2; }
?>

and, the main file:
<?php
require('./inc.php');
require('./inc.php');

if( function_exists('dummy') ) { echo 'function: yes'."\n"; }
else { echo 'function: no'."\n"; }
?>

Expected result:
----------------
Fatal error: Cannot redeclare dummy() (previously declared in inc.php:5) in inc.php on line 5


Actual result:
--------------
if the return statement is left uncommented:

function: yes

so the function exists despite the return.


if the return statement is commented:

Fatal error: Cannot redeclare dummy() (previously declared in inc.php:5) in inc.php on line 5

which should be the normal behaviour in both cases, i think.

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-02 02:11 UTC] christian dot lefebvre at atosorigin dot com
I'm testing php5 and I obtain what you expect as result, but i'd like the opposite :-)
  A lot of old php code use conditionnal return at
beginning of includefiles to simulate include_once
behavior. And this tips works no more with php5 because
all declared functions raise "already defined" errors.

  See my comments on http://bugs.php.net/bug.php?id=18590
for details
 [2004-07-27 23:14 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"If there are functions defined in the included file, they can be used in the main file independent if they are before return or after. If the file is included twice, PHP 5 issues fatal error because functions were already declared, while PHP 4 doesn't complain about it. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Aug 19 15:01:28 2024 UTC