1

下面的 PHP 函数产生了这个错误: Parse error: syntax error, unexpected '(', expecting T_STRING or T_VARIABLE or '$', on this line:throw new ( '' . 'Illigal name for service name "' . $name . '" is given' );

有谁知道出了什么问题?

function registerService($name, $provider) {
    if (strlen( $name ) < 1) {
        Exception;
        throw new ( '' . 'Illigal name for service name "' . $name . '" is given' );
    }

    $this->_Services[$name] = $provider;
}

谢谢

4

1 回答 1

1

改变这个:

    Exception;
    throw new ( '' . 'Illigal name for service name "' . $name . '" is given' );

对此:

throw new Exception('Illegal name for service name "' .$name .'" is given' );

此外,通常你会在 try catch 块中抛出一个异常:

try {
    throw new Exception('Just testing');
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
//Output: Caught exception: Just testing
于 2013-01-30T00:27:26.127 回答