常数运算式包含无效的运算子

编程入门 行业动态 更新时间:2024-10-22 15:33:06
本文介绍了常数运算式包含无效的运算子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码,出现错误"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'); } }

更多推荐

常数运算式包含无效的运算子

本文发布于:2023-10-14 22:22:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492385.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!