Reputation: 11887
Long story short, there is a PHP feature called ArrayAccess which allows you to overload the []
operator (among other things I think) for your class, so you could have a custom class MyClass implements ArrayAccess
of which $myVariable
is an Instance, and do stuff like
$myVariable[]=6;
or $someVar=$myVariable[78];
After you've defined what it'll behave like.
I find this feature rather rather elegant and useful and would like to know whether this is possible in other programming languages.
Thank you!
Upvotes: 1
Views: 87
Reputation: 816840
Python supports it too, by implementing the "magic" methods __getitem__
and __setitem__
.
With them you can even support slicing like obj[i:j]
.
Upvotes: 1
Reputation: 887837
C++ supports this in operator overloading.
Most .Net languages support this in indexers.
Upvotes: 2