[英]Setting undeclared property doesn't work in PHP 5.3.x?
誰能告訴我為什么:
class foo {
function __construct() {
foreach($_GET as $get_var => $val) {
$this->$get_var = $val;
}
foreach($_POST as $post_var => $val) {
$this->$post_var = $val;
}
$this->test = "test";
}
}
生成並清空對象,即
object(foo)#1 (0) {
}
然而:
class foo {
public $post;
public $get;
function __construct() {
foreach($_GET as $get_var => $val) {
$this->get->$get_var = $val;
}
foreach($_POST as $post_var => $val) {
$this->post->$post_var = $val;
}
}
}
工作正常,即:
object(foo)#1 (2) {
["post"]=>
NULL
["get"]=>
object(stdClass)#3 (2) {
["fred"]=>
string(4) "fish"
["joe"]=>
string(6) "bloggs"
}
}
這似乎只是PHP 5.3.x中的一個問題,但它在我們工作的計算機上的5.2.x中仍然有效。 是PHP配置問題還是我缺少什么? 我查看了“類和對象”文檔,但什么都看不到。
對於這個類來說,這不是什么大問題,但是在構建數據庫類時,這意味着需要做更多的工作。
是的,這是一個5.3問題。 實際上,您不應該依賴未聲明的變量用法,但這是一些解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.