Invalid operation causes constant expression to be invalid
P粉418854048
2023-08-22 20:09:10
<p>I have the following code and when I define a variable in the constructor I get the error "PHP Fatal Error: Constant expression contains an invalid operation". It works fine when used in Laravel framework. </p>
<pre class="brush:php;toolbar:false;"><?php
namespace App;
class Amazon
{
protected $serviceURL = config('api.amazon.service_url');
public function __construct()
{
}
}</pre>
<p>I saw this question: PHP error: fatal error: constant expression contains an invalid operation
But my code doesn't declare any static content, so this answer doesn't solve my problem. </p>
This method does not allow initialization of class attributes. You have to move the initialization into the constructor.
As described here
The only way you can make it work is: