PHP 致命错误:无法访问空属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14920216/
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 Fatal error: Cannot access empty property
提问by Bishan
I'm new to phpand I have executed below code. 
我是新手php,我已经执行了下面的代码。
<?php
class my_class{
    var $my_value = array();
    function my_class ($value){
        $this->my_value[] = $value;
    }
    function set_value ($value){
    // Error occurred from here as Undefined variable: my_value
        $this->$my_value = $value;
    }
}
$a = new my_class ('a');
$a->my_value[] = 'b';
$a->set_value ('c');
$a->my_class('d');
foreach ($a->my_value as &$value) {
    echo $value;
}
?>
I got below errors. What could be the error?
我得到以下错误。可能是什么错误?
Notice: Undefined variable: my_value in C:\xampp\htdocs\MyTestPages\f.php on line 15
Fatal error: Cannot access empty property in C:\xampp\htdocs\MyTestPages\f.php on line 15
回答by Philipp
You access the property in the wrong way. With the $this->$my_value = ..syntax, you set the property with the name of the value in $my_value. What you want is $this->my_value = ..
您以错误的方式访问该属性。使用$this->$my_value = ..语法,您可以使用 $my_value 中的值的名称设置属性。你想要的是$this->my_value = ..
$var = "my_value";
$this->$var = "test";
is the same as
是相同的
$this->my_value = "test";
To fix a few things from your example, the code below is a better aproach
要从您的示例中修复一些问题,下面的代码是一个更好的方法
class my_class {
    public  $my_value = array();
    function __construct ($value) {
        $this->my_value[] = $value;
    }
    function set_value ($value) {
        if (!is_array($value)) {
            throw new Exception("Illegal argument");
        }
        $this->my_value = $value;
    }
    function add_value($value) {
        $this->my_value = $value;
    }
}
$a = new my_class ('a');
$a->my_value[] = 'b';
$a->add_value('c');
$a->set_value(array('d'));
This ensures, that my_value won't change it's type to string or something else when you call set_value. But you can still set the value of my_value direct, because it's public. The final step is, to make my_value private and only access my_value over getter/setter methods
这可确保当您调用 set_value 时,my_value 不会将其类型更改为字符串或其他类型。但是你仍然可以直接设置 my_value 的值,因为它是公开的。最后一步是将 my_value 设为私有,并且只能通过 getter/setter 方法访问 my_value
回答by Marko D
First, don't declare variables using var, but
首先,不要使用 var 声明变量,但是
public $my_value;
Then you can access it using
然后你可以使用它访问它
$this->my_value;
and not
并不是
$this->$my_value;
回答by Val
回答by Abu Roma?ssae
As I see in your code, it seems you are following an old documentation/tutorial about OOP in PHP based on PHP4 (OOP wasn't supported but adapted somehow to be used in a simple ways), since PHP5 an official support was added and the notation has been changed from what it was.
正如我在您的代码中看到的那样,您似乎正在遵循有关基于 PHP4 的 PHP 中 OOP 的旧文档/教程(不支持 OOP,但以某种方式进行了调整,以便以简单的方式使用),因为添加了 PHP5 官方支持并且符号已经从原来的样子改变了。
Please see this code review here:
请在此处查看此代码:
<?php
class my_class{
    public $my_value = array();
    function __construct( $value ) { // the constructor name is __construct instead of the class name
        $this->my_value[] = $value;
    }
    function set_value ($value){
    // Error occurred from here as Undefined variable: my_value
        $this->my_value = $value; // remove the $ sign
    }
}
$a = new my_class ('a');
$a->my_value[] = 'b';
$a->set_value ('c'); // your array variable here will be replaced by a simple string 
// $a->my_class('d'); // you can call this if you mean calling the contructor 
// at this stage you can't loop on the variable since it have been replaced by a simple string ('c')
foreach ($a->my_value as &$value) { // look for foreach samples to know how to use it well
    echo $value;
}
?>
I hope it helps
我希望它有帮助
回答by gazdagergo
This way you can create a new object with a custom property name.
通过这种方式,您可以创建一个具有自定义属性名称的新对象。
$my_property = 'foo';
$value = 'bar';
$a = (object) array($my_property => $value);
Now you can reach it like:
现在你可以像这样到达它:
echo $a->foo;  //returns bar
回答by vikingmaster
Interesting:
有趣的:
- You declared an array  var $my_value = array();
- Pushed value into it    $a->my_value[] = 'b';
- Assigned a string to variable. (so it is no more array) $a->set_value ('c');
- Tried to push a value into array, that does not exist anymore. (it's string) $a->my_class('d');
- 你声明了一个数组  var $my_value = array();
- 将价值推入其中    $a->my_value[] = 'b';
- 将字符串分配给变量。(所以它不再是数组)$a->set_value ('c');
- 试图将一个值推送到数组中,该数组不再存在。(这是字符串)$a->my_class('d');
And your foreachwont work anymore.
你foreach不会再工作了。
回答by John Rix
I realise this answer is not a direct response to the problem described by the OP, but I found this question as a result of searching for the same error message. I thought it worth posting my experience here just in case anybody is muddling over the same thing...
我意识到这个答案不是对 OP 描述的问题的直接回应,但我在搜索相同的错误消息时发现了这个问题。我认为值得在这里发布我的经验,以防万一有人对同一件事感到困惑......
You can encounter the error in question as a result of a poorly formatted forloop over an associative array. In a fit of bone-headedness, I was using -> instead of => in my forstatement:
由于for关联数组上的错误格式循环,您可能会遇到有问题的错误。一头雾水,我在for声明中使用了 -> 而不是 => :
        foreach ($object->someArray as $key->$val) {
            // do something
        }
Of course, I should have had:
当然,我应该有:
        foreach ($object->someArray as $key=>$val) {
            // do something
        }
I confused myself at first, thinking the reported error was referring to the someArray property!
一开始我很困惑,以为报告的错误是指 someArray 属性!

