I have a class with an internal array, and I’d like to overload the subscript operator to access the internal array and provide debug/logging actions.
Is there any possible way to overload the [] operator in PHP, or is there another option I should be chasing in this case?
Yes, with the
ArrayAccessinterface. Although you won’t be able to cast the object to a proper array.With
ArrayObject(as mentioned in the duplicate question) you can however. Also see this comment on php.net on how to achieve this when you are extendingArrayObject.