1

我正在尝试使用 php 5.3.2 引发异常,它给了我以下错误:

解析错误:语法错误,意外的 T_THROW

我试图通过以下方式抛出异常:

throw new Exception('Property ' . $name . ' doesn\'t exist in class Index', '');

编辑:我也试过

throw new Exception('Property ' . $name . ' doesn\'t exist in class Index');

它并没有改变我得到的错误。

完整的方法:

public function __get($name) 
    {
        if(property_exists($this, $name)
            throw new Exception('Property ' . $name . ' doesn\'t exist in class Index');
        return $this->$name;
    }
4

2 回答 2

4

检查你的 php 代码中是否有花絮,有时我会错过“;” 这可能会导致错误。

还可以尝试正式编写 if 语句,并在括号中添加。我知道它不应该有任何赔率,但谁知道编程!

于 2010-04-08T10:28:35.293 回答
0

您缺少 if() 行的右括号,因此 PHP 看到了这一点

if(property_exists($this, $name) throw new Exception(...);

这是无效的语法。将 a ) 放在 if() 行的末尾:

if(property_exists($this, $name))

编辑:我讨厌错过答案下方的回复。:(

于 2010-06-07T23:39:16.113 回答