简体   繁体   English

PHP解析错误:语法错误,意外的“:”,期待“;” 或者 '{'

[英]PHP Parse error: syntax error, unexpected ':', expecting ';' or '{'

I'm try playing with php7.我正在尝试使用 php7。 I have installed https://github.com/rlerdorf/php7dev and connected through phpstorm.我已经安装了https://github.com/rlerdorf/php7dev并通过 phpstorm 连接。 I'm trying use new feature like this:我正在尝试使用这样的新功能:

<?php
namespace Kata;

class StringCalculator
{
    public function add(string $parameters): int {
        return 0;
    }
}

then I try to test it like this:然后我尝试像这样测试它:

<?php
namespace Kata;

class StringCalculatorTest extends \PHPUnit_Framework_TestCase
{
    public function testAddEmptyString()
    {
        $calc = new StringCalculator();
        $this->assertSame(0, $calc->add(''));
    }
}

and I launch with phpunit, unfortunately I have我用 phpunit 启动,不幸的是我有

PHP Parse error: syntax error, unexpected ':', expecting ';' PHP解析错误:语法错误,意外的“:”,期待“;” or '{'或者 '{'

Probably I haven't installed php7 properly but it seems to be ok when I php -v可能我没有正确安装 php7 但是当我 php -v 时似乎没问题

PHP 7.0.0-dev (cli) (built: May 25 2015 16:34:33) (DEBUG) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies PHP 7.0.0-dev (cli) (built: May 25 2015 16:34:33) (DEBUG) 版权所有 (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, 版权所有 (c) 1998-2015 Zend Zend OPcache v7.0.6-dev 技术,版权所有 (c) 1999-2015,由 Zend Technologies

UPDATE更新

Problem is not in installation/configuration php7 (I think), because when I run it from cli like this:问题不在于安装/配置 php7(我认为),因为当我像这样从 cli 运行它时:

<?php
    $calc = new Kata\StringCalculator();
    var_dump($calc->add(''));

outputs int(0) and no error.输出int(0)并且没有错误。

So maybe the problem is in phpunit?所以也许问题出在phpunit中?

From the error it is clear that there's a problem executing your code.从错误中可以清楚地看出执行您的代码存在问题。

PHP Parse error: syntax error, unexpected ':', expecting ';' or '{'

The problem in your case is most likely coming from PHPUnit since you have a correct php 7 setup.您的问题很可能来自 PHPUnit,因为您有正确的 php 7 设置。 If PHPUnit fails to understand your code it's gonna fail in the first bit of PHP 7 code which in your case using a return type declaration in this line如果 PHPUnit 无法理解您的代码,它将在 PHP 7 代码的第一位失败,在您的情况下,在此行中使用返回类型声明

public function add(string $parameters): int {

and it's gonna be triggered in your phpunit test when you try to assert.当您尝试断言时,它将在您的 phpunit 测试中触发。 If you comment your assersion the error should disappear.如果您评论您的断言,错误应该会消失。

I suggest verifiying the version of PHPUnit you are using, you should use version 5 or higher.我建议验证您正在使用的 PHPUnit 版本,您应该使用版本 5 或更高版本。 (preferably 6 for php 7.0) For more details about PHPunit versions, checkout this link . (对于 php 7.0 最好是 6)有关 PHPunit 版本的更多详细信息,请查看此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析错误:语法错误,意外的 &#39;[&#39;,期待 &#39;)&#39; php - Parse error: syntax error, unexpected '[', expecting ')' php 解析错误:语法错误,意外的&#39;[&#39;,期望&#39;)&#39;,可能是PHP版本问题 - Parse error: syntax error, unexpected '[', expecting ')', possibly PHP version issue Wordpress插件PHP解析错误:语法错误,意外的“ {”,预期为“)” - Wordpress plugin PHP Parse error: syntax error, unexpected '{', expecting ')' 解析错误:语法错误,意外的&#39;;&#39;,期望的是&#39;)&#39;(Wordpress functions.php) - Parse error: syntax error, unexpected ';', expecting ')' (Wordpress functions.php) PHP 版本 4:: 解析错误:语法错误,意外 '=',期待 ')' - PHP version 4 :: Parse error: syntax error, unexpected '=', expecting ')' PHP解析错误:语法错误,意外的T_IS_EQUAL,预期 - PHP Parse error: syntax error, unexpected T_IS_EQUAL, expecting 的PHP-语法错误,意外的&#39;(&#39;,期待&#39;)&#39; - php - syntax error, unexpected '(', expecting ')' 解析错误:语法错误,意外&#39;(&#39;,期待&#39;,&#39;或&#39;;&#39;in - Parse error: syntax error, unexpected '(', expecting ',' or ';' in 解析错误:语法错误,意外的&#39;{&#39;,需要&#39;(&#39;帮助? - Parse error: syntax error, unexpected '{', expecting '(' help? PHP解析错误:解析错误,意外的“ *”,期望为“}” - PHP Parse error: parse error, unexpected '*', expecting '}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM