Making A Collection With Magic Methods in PHP


A lot of PHP frameworks have something called “collections” (though, I’m sure some of them use a different term). Basically, these are special classes whose sole purpose is to hold a bunch of the same type of objects within it. They usually provide some extra functions for working with those objects and also a bit of data protection by making the contained objects themselves private or otherwise unavailable. A lot of this intrinsic functionality is provided by using magic methods.

I’m not going to cover every single bit of functionality that one might include in a respectable object collection class, but I would like to go over some neat tricks that you can pull off using magic methods.

Read more