I know I can loop through a PHP object and obtain the values of it’s members like so:
class MyObject {
public $one = 1;
public $two = 2;
public $three = 3;
function getMemberValues() {
foreach($this as $memb => $value) {
echo $value." ";
}
}
}
$o = new MyObject();
$o->getMemberValues();
// prints 1 2 3
but what I want to be able to do is loop through the members and assign a value to each one.
I can’t figure out the syntax for it though.
$this[$memb] = 111;
doesn’t work because you get a
Cannot use object of type MyObject as array
error, and
$this->$memb
obviously isn’t valid either.
Is this possible?
Many thanks
Yes,
$this->$membis valid.You can assign to a property like this:
This also works for functions, and this is documented here