[英]variable inside a variable in a php class
is it possible to copy a variable like this this? 是否可以复制这样的变量?
class Colours {
var $var = "one";
var $var2 = array('something', $var);
}
The preferable way is to do this in the constructor of the Colours
class. 最好的方法是在
Colours
类的构造函数中执行此操作。 I'm not sure in PHP, but in other languages the order of initialisation of the variables should not be relied upon. 我不确定在PHP中是什么,但是在其他语言中,不应该依赖于变量的初始化顺序。
class Colours
{
private $var;
private $var2;
public function __construct()
{
$this->var = "one";
$this->var2 = array('something', $this->var);
}
}
You'd need to use $this->var
to access the variable 您需要使用
$this->var
来访问变量
class Colours {
var $var = "one";
var $var2 = array('something', $this->var);
}
<?php
$var = "one";
$var2 = array('something', $var);
print_r($var2)
?>
I got the following output
Array
(
[0] => something
[1] => one
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.