PHP 类函数内的全局变量
为什么在类内部的函数内部,我不能执行此语句:
global $connected = true;
但我可以这样做:
global $connected;
$connected = true;
How come inside a function which is inside a class, I can't do this statement:
global $connected = true;
But I can do this:
global $connected;
$connected = true;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
$connected
引入作用域以及为其赋值是两件独立的事情。没有理由让它们在一份声明中成为可能,这实际上没有多大意义。
以下代码是否:
$x = 5
引入作用域?5
纳入作用域?5
分配给全局$x
?5
分配给全局$x
然后将$x
纳入作用域?我当然知道你的意思是后者,而前两者没有任何意义。但是,拟议的声明并不清楚这一点。这将是糟糕的语法。
The bringing of
$connected
into scope, and the assignment of a value to it, are two separate things.There is no reason for them to be possible in one statement, which wouldn't really make much sense.
Does the following code:
$x = 5
into scope?5
into scope?5
to the global$x
?5
to the global$x
and then bring$x
into scope?I know of course that you intend for it to mean the latter, and that the first two have no meaning. But, that is not clear from the proposed statement. It would be poor syntax.
因为在函数内部你首先必须声明一个全局变量。这是您必须在函数开始时执行的操作。这样你就可以激活某个未传递的变量。
Because inside a function you first have to announce a global variable. It is something you must do in the beginning of the function. That way you can activate a certain variable which was not passed through.