$GLOBALS 和大写 $var 的区别
Difference between $GLOBALS and uppercase $var
在 $GLOBALS 范围内声明变量和声明大写变量之间的区别是什么(如果有的话)?
我四处寻找答案,这里有一些包含有趣信息的链接,但我没有找到问题的答案。
PHP global in functions
what is the difference between GLOBALS and GLOBAL?
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/reserved.variables.globals.php
How to declare a global variable in php?
一个小测试告诉我们:
$FOO='BAR';
$GLOBALS['foo']='bar';
function ufoo(){ echo $GLOBALS['FOO']; }
function lfoo(){ echo $GLOBALS['foo']; }
ufoo(); //outputs BAR
lfoo(); //outputs bar
关于使用常量:这里有一些限制,所以它们对我不起作用。 http://php.net/manual/en/language.constants.php
此外,定义为对象或数组的常量将通过向我的错误日志添加行来占用磁盘 space:
class foo_bar{
var $a;
var $b;
}
$foo_bar_object=new foo_bar();
$foo_bar_object->a='foo';
$foo_bar_object->b='bar';
define('FOO_BAR_OBJECT',$foo_bar_object);
$foo_bar_array=array('foo','bar');
define('FOO_BAR_ARRAY',$foo_bar_array);
print_r(FOO_BAR_ARRAY);
print_r(FOO_BAR_OBJECT);
PHP Warning: Constants may only evaluate to scalar values
PHP
Warning: Constants may only evaluate to scalar values
PHP
Notice: Use of undefined constant FOO_BAR_ARRAY - assumed
'FOO_BAR_ARRAY'
PHP Notice: Use of undefined constant
FOO_BAR_OBJECT - assumed 'FOO_BAR_OBJECT'
请减少有关使用 $GLOBALS 的不良做法的话题。它在 php 中,如果使用得当,它会很有用。就像任何事情一样,并不是因为某些人滥用某些东西,每个人都应该避免它。
在全局范围内定义变量与将其定义为 $GLOBALS 超全局数组的 属性 相同。无论变量大小写如何,它都可以双向工作。
<?php
$test1 = "Hello World";
echo $GLOBALS["test1"];
echo "\n";
$GLOBALS["test2"] = "Goodbye World";
echo $test2;
// outputs
// Hello World
// Goodbye World
至于全局公开数组或对象(这几乎被普遍认为是不好的做法),您有多种选择。如您所述,define 和 const 仅适用于标量值,但您可以通过任何 PHP 超全局变量(包括 $GLOBALS)访问该对象。
或者您可以使用 Singleton anti pattern or a static class property。无论范围如何,所有这些都可以访问。
在 $GLOBALS 范围内声明变量和声明大写变量之间的区别是什么(如果有的话)?
我四处寻找答案,这里有一些包含有趣信息的链接,但我没有找到问题的答案。
PHP global in functions
what is the difference between GLOBALS and GLOBAL?
http://php.net/manual/en/language.variables.scope.php
http://php.net/manual/en/reserved.variables.globals.php
How to declare a global variable in php?
一个小测试告诉我们:
$FOO='BAR';
$GLOBALS['foo']='bar';
function ufoo(){ echo $GLOBALS['FOO']; }
function lfoo(){ echo $GLOBALS['foo']; }
ufoo(); //outputs BAR
lfoo(); //outputs bar
关于使用常量:这里有一些限制,所以它们对我不起作用。 http://php.net/manual/en/language.constants.php 此外,定义为对象或数组的常量将通过向我的错误日志添加行来占用磁盘 space:
class foo_bar{
var $a;
var $b;
}
$foo_bar_object=new foo_bar();
$foo_bar_object->a='foo';
$foo_bar_object->b='bar';
define('FOO_BAR_OBJECT',$foo_bar_object);
$foo_bar_array=array('foo','bar');
define('FOO_BAR_ARRAY',$foo_bar_array);
print_r(FOO_BAR_ARRAY);
print_r(FOO_BAR_OBJECT);
PHP Warning: Constants may only evaluate to scalar values
PHP Warning: Constants may only evaluate to scalar values
PHP Notice: Use of undefined constant FOO_BAR_ARRAY - assumed 'FOO_BAR_ARRAY'
PHP Notice: Use of undefined constant FOO_BAR_OBJECT - assumed 'FOO_BAR_OBJECT'
请减少有关使用 $GLOBALS 的不良做法的话题。它在 php 中,如果使用得当,它会很有用。就像任何事情一样,并不是因为某些人滥用某些东西,每个人都应该避免它。
在全局范围内定义变量与将其定义为 $GLOBALS 超全局数组的 属性 相同。无论变量大小写如何,它都可以双向工作。
<?php
$test1 = "Hello World";
echo $GLOBALS["test1"];
echo "\n";
$GLOBALS["test2"] = "Goodbye World";
echo $test2;
// outputs
// Hello World
// Goodbye World
至于全局公开数组或对象(这几乎被普遍认为是不好的做法),您有多种选择。如您所述,define 和 const 仅适用于标量值,但您可以通过任何 PHP 超全局变量(包括 $GLOBALS)访问该对象。
或者您可以使用 Singleton anti pattern or a static class property。无论范围如何,所有这些都可以访问。