Does Magic class work for multi-dimensional arrays or not?

31 views
Skip to first unread message

Summer White

unread,
May 1, 2017, 9:16:10 AM5/1/17
to Fat-Free Framework
Given myArray implements Magic.

> $myArray = new myArray();
> $myArray["a"] = "test";

set function gets called.

> $myArray = new myArray();
> $myArray["a"]["b"] = "test";

set function does not get called.

Does Magic not work for multi-dimensional arrays?

ikkez

unread,
May 1, 2017, 11:29:05 AM5/1/17
to Fat-Free Framework
is "a" defined and an array?

The set method is not always called,.. i.e.  with referencial usage, php updates the underlaying value "magically" without the setter.

could probably happen for

$myArray[] = 1;
$myArray[1]++;
$myArray[1]+=2;
$ref = &$myArray[1];
$ref = 2;

in the F3 mappers, which utilize that Magic class, these values get checked for changes upon update or insert.
The Magic class does it job well,.. when you fetch $myArray["a"]["b"], Magic just gives you 'a', which is already a nested array.

bcosca

unread,
May 1, 2017, 5:19:38 PM5/1/17
to Fat-Free Framework
@ikkez is right. set function does not get called when an array element is referenced. Instead, PHP invokes &get.

Summer White

unread,
May 2, 2017, 10:46:51 PM5/2/17
to Fat-Free Framework
After doing more research I'm learning that there are a considerable amount of issues when implementing Array Access for multi-dimensional arrays. It doesn't seem like its suited to it at all.

For my implementation I decided against it anyway and went with the conventional Java like $class->getThing() approach.
Reply all
Reply to author
Forward
0 new messages