Answer the question
In order to leave comments, you need to log in
How to create a property inside a method?
Tired of working with an array. I want to change the method
public function setData(array $data):void{
$this->data = $data;
}
public function setData(array $data):void{
foreach($data as $k => $v){
$this->$k = $v;
}
}
Answer the question
In order to leave comments, you need to log in
1. Do they do it?They do, but it's not good practice. Firstly, you have a chance to erase some of the object's configuration data and get unpredictable behavior. Secondly, if you have values from a database or other storage in the key-value format, it is better to store them in a separate field, the same date, but in the form of stdClass, so as not to confuse them with other fields when working with storage, like this:
public function setData(array $data):void{
$this->data = (object)$data;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question