Hello, inside the class there is a config task:

public $config = array( 'api_key' => '8a29125664722e968987b085cf3a', 'offer_id' => $offer_id, 'user_id' => 3291, ); 

Swears on the line with offer_id. What could be the problem?

  • Surely the variable is undefined. - Alexey Shatrov
  • @AlexeyShatrov no such error occurs if any sign was expected for example> this one. If a variable is not defined then an error of the level of the notice Undefined variable: offer_id - Naumov
  • Show the code above, at least the definition of the $offer_id variable and the class modifiers. - rjhdby

2 answers 2

Class fields cannot be initialized with non-constant expressions.
You can pass the desired value to a class method or to a constructor.

 class A { public $config = array( 'api_key' => '8a29125664722e968987b085cf3a', 'offer_id' => null, 'user_id' => 3291, ); public function __construct($offer_id) { $this->config['offer_id'] = $offer_id; } } 

But much better:

 class A { private $config; public function __construct($config) { $this->config = $config; } } 

    I would solve this problem like this.

     public function setConfigOffer($id){ if($id) $this->config['offer_id']=$id; else return false; } 

    and I would cross out this line - in my opinion it contradicts my ideas about creating classes

    • And I would off such a hand code: D plus is not the error - Naumov
    • @Naumov, please tell me an example - in which case a variable is indicated in the class property value, as far as I understand it when we write a class - we write a drawing, and after creating an instance of the class we can assign the variable value to the property of this instance. As a last resort, you can specify a reference to the variable in the class - but it looks very wild and it is not clear what purpose it is - Mcile