1

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

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.");

这是编译器.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");
4

2 回答 2

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]

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

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

于 2012-11-07T16:27:38.050 回答
3

你的数组是$this->compilers,不是$this->$compilers

$compilers在您的函数中不存在,因此$this->$compilers正在寻找一个空白属性。

于 2012-11-07T16:31:20.037 回答