不能将其他命名空间中的变量用作PHP中的默认函数参数

时间:2022-02-21 00:37:14

I'm using Laravel 5, in one class I define the function:

我正在使用Laravel 5,在一个类中我定义了这个函数:

<?php namespace App\Helpers;

use App\Helpers\CommonConst;

class ResTools {

    public static function resErr($data, $statusCode=CommonConst::$ERROR_CODES['BAD_REQUEST']){
        // Some code here
    }
}

And the CommonConst is:

CommonConst是:

class CommonConst {
    public static $ERROR_CODES = [
        'OK' => 200,
        'BAD_REQUEST' => 400,
        'UNAUTHORIZED' => 401,
        'FORBIDEN' => 403,
        'NOT_FOUND' => 404,
        'METHOD_NOT_ALLOWED' => 405,
        'INTERNAL_SERVER_ERROR' => 500,
    ];


}

When running I always got the error:

在运行时我总是得到错误:

syntax error, unexpected '$ERROR_CODES' (T_VARIABLE), expecting identifier (T_STRING)

How can I fix it?

我该如何解决?

1 个解决方案

#1


Unfortunately, your $ERROR_CODES variable is not a constant, it's a static variable. You cannot default the values of your method parameters to variables.

不幸的是,你的$ ERROR_CODES变量不是常量,它是一个静态变量。您不能将方法参数的值默认为变量。

Here's an example:

这是一个例子:

http://ideone.com/Hog5cD

#1


Unfortunately, your $ERROR_CODES variable is not a constant, it's a static variable. You cannot default the values of your method parameters to variables.

不幸的是,你的$ ERROR_CODES变量不是常量,它是一个静态变量。您不能将方法参数的值默认为变量。

Here's an example:

这是一个例子:

http://ideone.com/Hog5cD