我有以下代码,出现错误"PHP致命错误:常量表达式包含无效操作".当我在构造函数中定义变量时,它可以正常工作.我正在使用Laravel框架.
I have the following code, where I get the error "PHP Fatal Error: Constant expression contains invalid operations". It works fine when I define the variable in the constructor. I am using Laravel framework.
<?php namespace App; class Amazon { protected $serviceURL = config('api.amazon.service_url'); public function __construct() { } }我已经看到以下问题: PHP错误:致命错误:常量表达式包含无效的操作 但是我的代码没有将任何东西声明为静态的,因此没有回答我的问题.
I have seen this question: PHP Error : Fatal error: Constant expression contains invalid operations But my code does not declare anything as static, so that did not answer my question.
推荐答案如这里
类成员变量称为属性".您可能还会看到使用其他术语(例如属性"或字段")来引用它们,但是出于参考目的,我们将使用属性".它们通过使用关键字public,protected或private之一定义,后跟普通变量声明.该声明可以包括一个初始化,但是此初始化必须是一个常量值-也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估.
Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
完成这项工作的唯一方法是:-
The only way you can make this work is :-
<?php namespace App; class Amazon { protected $serviceURL; public function __construct() { $this->serviceURL = config('api.amazon.service_url'); } }更多推荐
常数运算式包含无效的运算子
发布评论