gpt4 book ai didi

php - 常量表达式包含无效操作

转载 作者:行者123 更新时间:2023-12-02 19:57:59 32 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





PHP Error : Fatal error: Constant expression contains invalid operations

(5 个回答)


3年前关闭。




我有以下代码,其中出现错误“PHP fatal error :常量表达式包含无效操作”。当我在构造函数中定义变量时它工作正常。我正在使用 Laravel 框架。

<?php

namespace App;

class Amazon
{
protected $serviceURL = config('api.amazon.service_url');

public function __construct()
{
}

}

我看过这个问题: PHP Error : Fatal error: Constant expression contains invalid operations
但是我的代码没有将任何内容声明为静态,因此没有回答我的问题。

最佳答案

如所述here

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.



完成这项工作的唯一方法是:-
<?php

namespace App;

class Amazon
{
protected $serviceURL;

public function __construct()
{
$this->serviceURL = config('api.amazon.service_url');
}
}

关于php - 常量表达式包含无效操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56441748/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com