Class 构造函数使用变量名创建新的 class 字段?
Class constructor creates new class fields with variable names?
我正在尝试通过 PHP 5.5.13 的构造函数初始化 class,但得到了一些奇怪的结果。设置是这样的:
class foo{
public $bar;
public $top;
public $bot = array();
function __construct($bar, $top){
$this->$bar = $bar;
$this->$top = $top;
}
}
$phonebook = array();
$user_input = $_POST['query'];
if(/* regex match */){
foreach($valid_input[0] as $arr){
$name_and_number = explode(" ", $arr);
$phonebook[] = new foo($name_and_number[0], (int) $name_and_number[1]); //e.g. Bob, 123
var_dump($phonebook[count($phonebook)-1]);
}
}
现在奇怪的是,电话簿的 var_dump returns:
object(foo)#1 (5) { ["bar"]=> NULL ["top"]=> NULL ["bot"]=> array(0) { }
["Bob"]=> string(3) "Bob" ["123"]=> int(123) }
运行:
echo "$phonebook[0]->$bar";
echo "$phonebook[0]['Bob']"; //Since a Bob field apparently exists?
echo "$phonebook[0]->$Bob"; //Just to test if maybe a $Bob variable has been declared?
全部return一个空白页。我在这里不知所措。我的构造函数设置很奇怪吗?或者我尝试访问变量的方式?
你需要做的就是像这样摆脱第二个 $
标志
class foo{
public $bar;
public $top;
public $bot = array();
function __construct($bar, $top){
$this->bar = $bar;
$this->top = $top;
}
}
您看到 'weird' 结果的原因是 $bar
和 $top
的值是动态计算的,将用于创建一个新的名为 属性.在您的情况下,导致名为 'Bob' 和 '123'
的 属性
问题出在这些行中:
function __construct($bar, $top){
$this->$bar = $bar;
$this->$top = $top;
}
$this->$bar
是指以 柱的 值命名的 属性。因此,如果您传递名称 Bob
,您实际上将 属性 Bob
设置为 'Bob'
。
你的本意当然是设置属性bar
。为此,删除 $
符号。属性必须省略:
$this->bar = $bar;
所以它与构造函数无关,它只是你在任何地方使用属性的方式。在构造函数中,甚至在 class 方法之外。 echo "$phonebook[0]->$bar"
也应该是 echo "$phonebook[0]->bar";
.
我个人认为这是一种奇怪且违反直觉的语法,但我曾与 PHP 爱好者为之激烈争论过,所以我不敢再提起它。只是忍受它。 ;)
我正在尝试通过 PHP 5.5.13 的构造函数初始化 class,但得到了一些奇怪的结果。设置是这样的:
class foo{
public $bar;
public $top;
public $bot = array();
function __construct($bar, $top){
$this->$bar = $bar;
$this->$top = $top;
}
}
$phonebook = array();
$user_input = $_POST['query'];
if(/* regex match */){
foreach($valid_input[0] as $arr){
$name_and_number = explode(" ", $arr);
$phonebook[] = new foo($name_and_number[0], (int) $name_and_number[1]); //e.g. Bob, 123
var_dump($phonebook[count($phonebook)-1]);
}
}
现在奇怪的是,电话簿的 var_dump returns:
object(foo)#1 (5) { ["bar"]=> NULL ["top"]=> NULL ["bot"]=> array(0) { }
["Bob"]=> string(3) "Bob" ["123"]=> int(123) }
运行:
echo "$phonebook[0]->$bar";
echo "$phonebook[0]['Bob']"; //Since a Bob field apparently exists?
echo "$phonebook[0]->$Bob"; //Just to test if maybe a $Bob variable has been declared?
全部return一个空白页。我在这里不知所措。我的构造函数设置很奇怪吗?或者我尝试访问变量的方式?
你需要做的就是像这样摆脱第二个 $
标志
class foo{
public $bar;
public $top;
public $bot = array();
function __construct($bar, $top){
$this->bar = $bar;
$this->top = $top;
}
}
您看到 'weird' 结果的原因是 $bar
和 $top
的值是动态计算的,将用于创建一个新的名为 属性.在您的情况下,导致名为 'Bob' 和 '123'
问题出在这些行中:
function __construct($bar, $top){
$this->$bar = $bar;
$this->$top = $top;
}
$this->$bar
是指以 柱的 值命名的 属性。因此,如果您传递名称 Bob
,您实际上将 属性 Bob
设置为 'Bob'
。
你的本意当然是设置属性bar
。为此,删除 $
符号。属性必须省略:
$this->bar = $bar;
所以它与构造函数无关,它只是你在任何地方使用属性的方式。在构造函数中,甚至在 class 方法之外。 echo "$phonebook[0]->$bar"
也应该是 echo "$phonebook[0]->bar";
.
我个人认为这是一种奇怪且违反直觉的语法,但我曾与 PHP 爱好者为之激烈争论过,所以我不敢再提起它。只是忍受它。 ;)