note 55595 added to language.variables.scope
| From: | thomasatpixturdotde at osu1 dot php dot net | Date: | Mon, 08 Aug 2005 15:02:07 +0000 |
| Subject: | note 55595 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93250@lists.php.net to get a copy of this message | ||
Be careful with "require", "require_once" and "include" inside
functions. Even if the included file seems to define global variables, they might not be defined as
such.
consider those two files:
---index.php------------------------------
function foo() {
require_once("class_person.inc");
$person= new Person();
echo $person->my_flag; // should be true, but is undefined
}
foo();
---class_person.inc----------------------------
$seems_global=true;
class Person {
public $my_flag;
public function __construct() {
global $seems_global;
$my_flag= $seems_global
}
}
---------------------------------
The reason for this behavior is quiet obvious, once you figured it out. Sadly this might not be
always as easy as in this example. A solution would be to add the line...
global $seems_global;
at the beginning of "class_person.inc". That makes sure you set the global-var.
best regards
tom
ps: bug search time approx. 1 hour.
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55595
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55595&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55595&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55595&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55595&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55595&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55595&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55595&report=yes
Search -- http://master.php.net/manage/user-notes.php