我需要从另一个函数访问全局变量。首先,我已经在一个函数中将值赋给了全局变量。当我试图从另一个函数获取该值时,它总是返回null。在这里我的代码是全局变量不能在函数内部访问
StockList.php
<?php
$_current;
class StockList
{
public function report(){
global $_current;
$_current = 10;
}
public function getValue(){
print_r($GLOBALS['_current']);
}
}
?>
Suggestion.php
<?php
include ("StockList.php");
$stk = new StockList();
$stk->getValue();
?>
在此先感谢。
为什么这需要是一个全局变量?为什么你不能简单地使用类属性? –
在创建之前,您无法访问全局变量。这样说,尽量避免全局。 – Andrew
我觉得全局变量应该在类内 –