If I understood correctly what you want to achieve, you could use a closure:
class kittengarten
{
var $cats;
function __construct()
{
$this->cats[0]='Jerry';
$this->cats[1]='John';
$this->cats[2]='Barack';
}
public function __get($var)
{
if($var == "CAT")
{
$array_of_cats=$this->cats;
return function($num) use ($array_of_cats)
{
return $array_of_cats[$num];
};
}
}
}
$kittengarten=new kittengarten();
echo 'The third directly accessed cat is '.$kittengarten->cats[2];
$cat=$kittengarten->CAT;
if (is_string($cat)) echo $kittengarten->CAT;
else echo 'The third cat accessed using closure is '.$cat(2);
Output:
The third directly accessed cat is Barack
The third cat accessed using closure is Barack
$cat_idsince it will be provided, after__getreturned a value$obj->CAT[15]will be valid syntax. See the complete list of changes. Also check out this nice overview of new features$obj->CAT[15]is already valid syntax. The new array dereferencing stuff covers array-like values returned from functions, like:foo()[0].