PHP 类:全局变量作为类中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4489134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP class: Global variable as property in class
提问by Zebra
I have a global variable outside my class = $MyNumber;
我在班级之外有一个全局变量 = $MyNumber;
How do I declare this as a property in myClass?
For every method in my class, this is what I do:
如何将其声明为myClass 中的属性?
对于我班上的每个方法,这就是我所做的:
class myClass() {
private function foo() {
$privateNumber = $GLOBALS['MyNumber'];
}
}
I want this
我要这个
class myClass() {
//What goes here?
var $classNumber = ???//the global $MyNumber;
private function foo() {
$privateNumber = $this->classNumber;
}
}
EDIT:I want to create a variable based on the global $MyNumber but
modified before using it in the methods
编辑:我想基于全局 $MyNumber 创建一个变量,但
在方法中使用它之前进行了修改
something like: var $classNumber = global $MyNumber + 100;
类似于: var $classNumber = global $MyNumber + 100;
回答by El Yobo
You probably don'treally want to be doing this, as it's going to be a nightmare to debug, but it seems to be possible. The key is the part where you assign by referencein the constructor.
你可能不真要这么做,因为这将是一场噩梦调试,但它似乎是不可能的。关键是您在构造函数中通过引用分配的部分。
$GLOBALS = array(
'MyNumber' => 1
);
class Foo {
protected $glob;
public function __construct() {
global $GLOBALS;
$this->glob =& $GLOBALS;
}
public function getGlob() {
return $this->glob['MyNumber'];
}
}
$f = new Foo;
echo $f->getGlob() . "\n";
$GLOBALS['MyNumber'] = 2;
echo $f->getGlob() . "\n";
The output will be
输出将是
1
2
which indicates that it's being assigned by reference, not value.
这表明它是通过引用而不是值来分配的。
As I said, it will be a nightmare to debug, so you really shouldn't do this. Have a read through the wikipedia article on encapsulation; basically, your object should ideally manage its own data and the methods in which that data is modified; even public properties are generally, IMHO, a bad idea.
正如我所说,调试将是一场噩梦,所以你真的不应该这样做。通读维基百科关于封装的文章;基本上,理想情况下,您的对象应该管理自己的数据以及修改该数据的方法;恕我直言,即使是公共财产也是一个坏主意。
回答by mrwooster
Try to avoid globals, instead you can use something like this
尽量避免使用全局变量,而您可以使用这样的方法
class myClass() {
private $myNumber;
public function setNumber($number) {
$this->myNumber = $number;
}
}
Now you can call
现在你可以打电话
$class = new myClass();
$class->setNumber('1234');
回答by John Parker
Simply use the global keyword.
只需使用global 关键字。
e.g.:
例如:
class myClass() {
private function foo() {
global $MyNumber;
...
$MyNumber will then become accessible (and indeed modifyable) within that method.
然后 $MyNumber 将在该方法中变得可访问(并且实际上是可修改的)。
However, the use of globals is often frowned upon (they can give off a bad code smell), so you mightwant to consider using a singleton class to store anything of this nature. (Then again, without knowing more about what you're trying to achieve this might be a very bad idea - a definecould well be more useful.)
然而,全局变量的使用通常是不受欢迎的(它们会散发出不好的代码味道),因此您可能需要考虑使用单例类来存储这种性质的任何东西。(再说一次,如果不了解更多关于您要实现的目标,这可能是一个非常糟糕的主意 -定义可能更有用。)
回答by emre
What I've experienced is that you can't assign your global variable to a class variable directly.
我所经历的是,您不能将全局变量直接分配给类变量。
class myClass() {
public $var = $GLOBALS['variable'];
public function func() {
var_dump($this->var);
}
}
With the code right above, you get an error saying "Parse error: syntax error, unexpected '$GLOBALS'"
使用上面的代码,您会收到一条错误消息“解析错误:语法错误,意外的 '$GLOBALS'”
But if we do something like this,
但是如果我们做这样的事情,
class myClass() {
public $var = array();
public function __construct() {
$this->var = $GLOBALS['variable'];
}
public function func() {
var_dump($this->var);
}
}
Our code will work fine.
我们的代码会正常工作。
Where we assign a global variable to a class variable must be inside a function. And I've used constructor function for this.
我们将全局变量分配给类变量的地方必须在函数内部。我为此使用了构造函数。
So, you can access your global variable inside the every function of a class just using $this->var;
因此,您可以使用$this->var访问类的每个函数内的全局变量;
回答by Petr Peller
What about using constructor?
使用构造函数怎么样?
class myClass {
$myNumber = NULL;
public function __construct() {
global myNumber;
$this->myNumber = &myNumber;
}
public function foo() {
echo $this->myNumber;
}
}
Or much better this way (passing the global variable as parameter when inicializin the object - read only)
或者这样更好(在初始化对象时将全局变量作为参数传递 - 只读)
class myClass {
$myNumber = NULL;
public function __construct($myNumber) {
$this->myNumber = $myNumber;
}
public function foo() {
echo $this->myNumber;
}
}
$instance = new myClass($myNumber);
回答by Gal
If you want to access a property from inside a class you should:
如果您想从类内部访问属性,您应该:
private $classNumber = 8;
回答by Xerxes
class myClass { protected $foo;
class myClass { protected $foo;
public function __construct(&$var)
{
$this->foo = &$var;
}
public function foo()
{
return ++$this->foo;
}
}
}