Reputation: 4719
Say we have the object $teams
(an associative array) containing an object that provides the method getMembers()
which returns an Array as follows:
$membersArray = $teams['blueteam']->getMembers();
If I want to then access individual members, I may do so as follows:
$membersArray[1];
Why can't I perform the access in-line as follows, and is there a proper way to do so in PHP?
$membersArray = $teams['blueteam']->getMembers()[1];
Upvotes: 0
Views: 60
Reputation: 78994
Support for this added in PHP 5.4.0:
$membersArray = $teams['blueteam']->getMembers()[1];
Upvotes: 1
Reputation: 24549
Rather than try to access it like that, why not make an alternative method called getMember()
which accepts a parameter for the array index. For example:
function getMember( $index )
{
return $this->members[$index];
}
This makes your code a little more self-documenting by indicating getMembers
will return an array of members, where getMember()
will only return a single array element.
Upvotes: 1