I have a class like this:
class Foo {
$elements = array();
function getElementByName($name) {
foreach($this->elements as $elm) {
if ($elm->name == $name) {
return $elm;
}
}
}
}
I expected the following code to modify the element of my array:
$myFoo = new Foo();
$myFoo->getElementByName('foo1')->active = true;
Instead, when running my code, the active
property of $elements['foo1']
is still false as it was before calling getElementByName
I think that the function makes a "copy" of the element, how can I get the real element of the array, so that when I modify it, and then access it in the array, its values have changed?
Return a reference to it (notice the &):
function &getElementByName($name) { ... }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments