php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30418 can't get values from a class's method when include this class that use global
Submitted: 2004-10-13 04:13 UTC Modified: 2004-10-13 09:22 UTC
From: tonerzhang at hotmail dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.3.8 OS: windows2000 server sp4
Private report: No CVE-ID: None
 [2004-10-13 04:13 UTC] tonerzhang at hotmail dot com
Description:
------------
Hello,
Excuse my too poor english!

question:
can't get values from a class's method when include this class that use global variable.

please run the exemple:test1.php and exemple:test2.php
why?

exemple:
test1.php 
<?php 
function showVar() { 
include 'class_test.php'; 
   $obc   = new testClass(); 
   $obc->showVar(); 
} 
showVar(); 
?>
test2.php
<?php 
include 'class_test.php'; 
function showVar() { 
   $obc   = new testClass(); 
   $obc->showVar(); 
} 
showVar(); 
?>
class_test.php
<?php 
$tmpVar   = "test data"; 
class testClass { 
   function showVar() { 
      global $tmpVar; 
      var_dump($tmpVar); 
   } 
} 
?>

Reproduce code:
---------------
class_test.php
<?php 
$tmpVar   = "test data"; 
class testClass { 
   function showVar() { 
      global $tmpVar; 
      var_dump($tmpVar); 
   } 
} 
?>
test1.php 
<?php 
function showVar() { 
include 'class_test.php'; 
   $obc   = new testClass(); 
   $obc->showVar(); 
} 
showVar(); 
?>

Expected result:
----------------
string:"test data"

Actual result:
--------------
NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-13 09:22 UTC] derick@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

No bug here, you include the class inside a function, so the variable is not put in the global scope but in the the function that called the include. 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 17:01:29 2024 UTC