php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #15065 include behaviour
Submitted: 2002-01-16 06:54 UTC Modified: 2002-01-16 09:28 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: tit dot petric at telemach dot net Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.6 OS: im guessing, all
Private report: No CVE-ID: None
 [2002-01-16 06:54 UTC] tit dot petric at telemach dot net
If the include() occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function.

<< this is the current behaviour, straight out of the manual for include(),..

now, this gets tricky, if you have function includes over 3-4-5 or more levels of includes, for example

index.php calls subfiles() which includes blah.php which calls modules.php which includes file.php and so on..

usage of global variables in theese files forces people to use global definitions like in functions on a global-current-file-scope, or in one of the lower levels in functions,..

maybe if would be a good idea to make a php.ini switch like force_global_includes to change this behaviour, so the variables in the includes always get defined in the global scope?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-16 06:57 UTC] tit dot petric at telemach dot net
it probably applies to all versions of php, atleast 4.x :)
 [2002-01-16 07:08 UTC] hholzgra@php.net
please use the special array $GLOBALS[]
 [2002-01-16 07:30 UTC] tit dot petric at telemach dot net
even with $GLOBALS or $_GLOBALS in 4.1, it still only reduces the problem by a larage part, but still enough of it remains visible.

i dont want to use $_GLOBALS['varname'] for variable definitions outside functions (in global scope), to make it global on any depth of include(), when it seems very simple to add a switch to change include()'s behaviour, so that all variables set outside the functions would/could automatically be considered global.

i know with 4.1 its easier to bypass this with php code, but you could just remove $_GLOBAL[] outside the functions alltogether if you wanted to set(use?) global variables.
 [2002-01-16 09:28 UTC] hholzgra@php.net
a php.ini setting would only lead to additional confusion IMHO

what about declaring your globals as "global $var;"
once in your include files? shouldn't harm if executed
in global scope and should make stuff global if 
included in function scope? (untested)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 27 19:01:29 2024 UTC