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

查看:61
本文介绍了PHP解析错误:语法错误,意外的“:",期待“;"或者 '{'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 php7.我已经安装了 https://github.com/rlerdorf/php7dev 并通过 phpstorm 连接.我正在尝试使用这样的新功能:

I'm try playing with php7. I have installed https://github.com/rlerdorf/php7dev and connected through 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(''));
    }
}

我用 phpunit 启动,不幸的是我有

and I launch with phpunit, unfortunately I have

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

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

可能我没有正确安装 php7,但是当我 php -v 时似乎没问题

Probably I haven't installed php7 properly but it seems to be ok when I php -v

PHP 7.0.0-dev (cli) (built: May 25 2015 16:34:33) (DEBUG) 版权所有(c) 1997-2015 PHP Group Zend Engine v3.0.0-dev,版权所有 (c)1998-2015 Zend Technologies使用 Zend OPcache v7.0.6-dev,版权所有 (c) 1999-2015,由 Zend Technologies 提供

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

更新

问题不在于安装/配置 php7(我认为),因为当我像这样从 cli 运行它时:

Problem is not in installation/configuration php7 (I think), because when I run it from cli like this:

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

输出 int(0) 并且没有错误.

outputs int(0) and no error.

所以也许问题出在 phpunit 上?

So maybe the problem is in phpunit?

推荐答案

从错误中可以明显看出执行您的代码时出现问题.

From the error it is clear that there's a problem executing your code.

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

您的问题很可能来自 PHPUnit,因为您有正确的 php 7 设置.如果 PHPUnit 无法理解您的代码,它将在 PHP 7 代码的第一位失败,在您的情况下使用 返回类型声明在这一行

The problem in your case is most likely coming from PHPUnit since you have a correct php 7 setup. 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

public function add(string $parameters): int {

当您尝试断言时,它将在您的 phpunit 测试中触发.如果您评论您的断言,错误应该会消失.

and it's gonna be triggered in your phpunit test when you try to assert. If you comment your assersion the error should disappear.

我建议验证您使用的 PHPUnit 版本,您应该使用版本 5 或更高版本.(对于 php 7.0 最好是 6)有关 PHPunit 版本的更多详细信息,请查看此链接.

I suggest verifiying the version of PHPUnit you are using, you should use version 5 or higher. (preferably 6 for php 7.0) For more details about PHPunit versions, checkout this link.

这篇关于PHP解析错误:语法错误,意外的“:",期待“;"或者 '{'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆