gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:10:40 27 4
gpt4 key购买 nike

我有以下代码,其中出现错误“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/40827870/

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