php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26174 Global vs. class vs. variable scope w/ include
Submitted: 2003-11-08 02:30 UTC Modified: 2003-11-08 11:55 UTC
From: myle34 at hotmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.0.0b2 (beta2) OS: Windows XP Home
Private report: No CVE-ID: None
 [2003-11-08 02:30 UTC] myle34 at hotmail dot com
Description:
------------
This is related to Bug #26173 (http://bugs.php.net/bug.php?id=26173) but the reproduce code was wrong.

I am using Windows XP Home and Apache 2.0.47 with PHP beta 2.

This bug is related to function/class/global scopes.

Note that if you merge foo.php and bar.php (foo.php and right after that bar.php) (ie. without the include_once), this will work as expected!

I haven't tested this with include but I suspect it would have the same problem.

Reproduce code:
---------------
// foo.php
<?php
class TestOne {
  function __construct() { include_once 'bar.php'; }
}
$TestOne = new TestOne();
?>

// bar.php
<?php
// uncommenting this makes it work
// global $foo;
$foo = 'bar';
// uncommenting this does nothing
// global $foo;
function TestTwo() {
  global $foo; echo $foo;
}
Tesl'><();
?>

Expected result:
----------------
bar

Actual result:
--------------
No output

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-08 11:55 UTC] alan_k@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

when you include a file within a function/method , the global code is in the function/method scope.

therefore $foo is in the function scope... (not global)..

functions/classes/methods you define in there however are defined globaly..
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 10 07:01:28 2024 UTC