PHP无法访问空属性错误

时间:2012-11-07 16:25:49

标签: php

我收到以下错误,我似乎无法弄清楚为什么或如何触发它。

Fatal error: Cannot access empty property in /home/content/p/l/a/plai1870/html/com/php/Bone/Compiler.php on line 18

第18行

throw new LogicException($this->$compilers[$language]." is not a supported compiler.");

这是Compiler.php

<?php
namespace Bone;
use LogicException;

class Compiler implements \Bone\Interfaces\Compiler {

    protected $compiler;

    protected $compilers = array(
        "php"           => "PHP",
        "as3"           => "ActionScript3",
        "javascript"    => "Javascript"
    );

    public function __construct($language) {
        $language = strtolower($language);
        if (!isset($this->$compilers[$language])) {
            throw new LogicException($this->$compilers[$language]." is not a supported compiler.");
        }
        $compiler = "\Bone\Compilers\\".$this->$compilers[$language]."\Compiler";
        $this->compiler = new $compiler();
    }

    public function buildDefinition($object, $path = null) {
        return $this->compiler()->buildInterface($object, $path);
    }

    public function buildObject($object, $path = null) {
        return $this->compiler->buildObject($object, $path);
    }   

    public function parameters($method) {
        return;
    }

    public function save($data, $path) {
        return;
    }
}
?>

修改 我用它来称呼它:

$compiler = new \Bone\Compiler("php");

2 个答案:

答案 0 :(得分:7)

对不起,如果这是最明显的,但是:

throw new LogicException($this->$compilers[$language]." is not a supported compiler.");

由于已检查该属性不存在,因此不应该:

throw new LogicException("$language is not a supported compiler.");

修改

$this->$compilers[$language]
       ^- variable property

删除那里的$

$this->compilers[$language]

然后你可以检查是否设置了数组中的条目,而不是设置了(unset)数组$compilers(局部变量)中具有值名称的属性。

开发时,请始终打开警告和通知(您可以想象的最高错误级别),以便在没有PHP首先警告您的情况下不会遇到这些问题。

答案 1 :(得分:3)

您的数组是$this->compilers,而不是$this->$compilers

您的函数中不存在

$compilers,因此$this->$compilers正在寻找空白属性。