简体   繁体   English

设置未声明的属性在PHP 5.3.x中不起作用吗?

[英]Setting undeclared property doesn't work in PHP 5.3.x?

Can anyone tell me why: 谁能告诉我为什么:

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";
    }
}

Generates and empty object ie 生成并清空对象,即

object(foo)#1 (0) {   
}

yet: 然而:

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;
        }
    }
}

Works just fine ie: 工作正常,即:

object(foo)#1 (2) {
  ["post"]=>
  NULL
  ["get"]=>
  object(stdClass)#3 (2) {
    ["fred"]=>
    string(4) "fish"
    ["joe"]=>
    string(6) "bloggs"
  }
}

It only seems to be an issue in PHP 5.3.x yet it works in 5.2.x on our machines at work. 这似乎只是PHP 5.3.x中的一个问题,但它在我们工作的计算机上的5.2.x中仍然有效。 Is it a PHP config issue or am I missing something? 是PHP配置问题还是我缺少什么? I looked in the Classes and Objects docs and can't see anything. 我查看了“类和对象”文档,但什么都看不到。

It's not a big issue for this class but when it comes to building database classes it means a LOT more work. 对于这个类来说,这不是什么大问题,但是在构建数据库类时,这意味着需要做更多的工作。

yes, it's a 5.3 issue. 是的,这是一个5.3问题。 actually you shouldn't be depending on undeclared variable usage but here is some solutions. 实际上,您不应该依赖未声明的变量用法,但这是一些解决方案。

http://www.tonylake.info/?p=159 http://www.tonylake.info/?p=159

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM