I have an array of objects $comments
.
I get it through calling TableGateway wise call to database.
$comments = $this->getCommentsTable()->getComments($theme_id);
I now need to change one of the properties of array object elements:
foreach ($comments as $comment) {
$comment->liked = 1;
}
But the value of $comment->liked
doesn't change.
I now need to add a new property disliked:
foreach ($comments as $comment) {
$comment->disliked = 1;
}
But it doesn't add (I know it by the following error):
( ! ) Notice: Undefined property: My\Model\Comments::$disliked in C:\my\project\module\my\view\Name\operation\comment-info-panel.phtml on line 31
Call Stack
# Time Memory Function Location
1 0.0008 255368 {main}( ) ..\index.php:0
2 0.1599 6312880 Zend\Mvc\Application->run( ) ..\index.php:26
3 1.8049 9176328 Zend\Mvc\Application->completeRequest( ) ..\Application.php:322
4 1.8049 9176520 Zend\EventManager\EventManager->trigger( ) ..\Application.php:347
5 1.8050 9176648 Zend\EventManager\EventManager->triggerListeners( )
I tried the following with reference:
foreach ($comments as &$comment) {
$comment->disliked = 1;
}
But I get a nasty error:
( ! ) Fatal error: An iterator cannot be used with foreach by reference in C:\My\Project\module\Name\src\Name\Controller\MyController.php on line 804
Call Stack
# Time Memory Function Location
1 0.0010 255368 {main}( ) ..\index.php:0
2 0.1601 6312880 Zend\Mvc\Application->run( ) ..\index.php:26
Thanks to find a way to modify and/or add a property on an object in this array.