I have a problem to get an object from an array-collection of objects by ID.
The following is my code:
protected $_rootLocation;
public function __construct(Location $rootLocation)
{
$this->_rootLocation= $rootLocation;
var_dump($this->_rootLocation);
}
public function getLocationById($id)
{
$value = null;
foreach($this->_rootLocationas $root)
{
if ($id == $root->getId())
{
$value = $root;
break;
}
}
return $value;
}
Then the function return “NULL” so it’s dosn’t work…
Edit
I do like that :
$manager = new LocationManager($rootLocation);
echo "<pre>";
var_dump($manager->getLocationById('291'));
echo "</pre>";
Your function returns null because the object is not found!
It depends on the implementation of the
myClasseobject, this must implement theiteratorinterface and thegetId()method must return a valid Id on each iteration.