[英]Can not use variable from other namespace as default function parameter in PHP
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? 我该如何解决?
Unfortunately, your $ERROR_CODES
variable is not a constant, it's a static variable. 不幸的是,您的$ERROR_CODES
变量不是常量,而是静态变量。 You cannot default the values of your method parameters to variables. 您不能将方法参数的值默认为变量。
Here's an example: 这是一个例子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.