Invalid operation causes constant expression to be invalid
P粉418854048
P粉418854048 2023-08-22 20:09:10
0
2
533
<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>
P粉418854048
P粉418854048

reply all(2)
P粉391955763

This method does not allow initialization of class attributes. You have to move the initialization into the constructor.

P粉652523980

As described here

The only way you can make it work is:

<?php

namespace App;

class Amazon
{
  protected $serviceURL;

  public function __construct()
  {
    $this->serviceURL = config('api.amazon.service_url');
  }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template