I am trying to create class properties for each element in an array. The array is already created at this point, I’m just creating it here for demonstration purposes.
I’m not too familiar with classes and objects. Could someone help me out?
class MyClass
{
$days['first'] = "Mon";
$days['second'] = "Tue";
$days['third'] = "Wed";
foreach ($days as $k => $v) {
public $k = $v;
}
}
$obj = new MyClass;
echo $obj->first; // Mon
I keep getting “Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION”.
Use a constructor to set the properties dynamically using
$this->$keyfrom an array that is passed in as a parameter, like so:$this->$kis a type of variable-variables where you can set the name of a variable using the value of another variable.