08 Dec 2012
Creating Java-Like Nested Classes in PHP
So a buddy of mine and I were trying to figure out how to make our PHP class for connecting to our MongoDB do something like:
and we found out that you can’t nest classes with PHP like you can in some other programming languages. Our solution was to create a class with public variables that would hold the other classes.
Therefore now we can do something in the posts class like:
and then be able to call it in a nicer fashion like this:
Rather than something like this:
This tactic for nesting PHP classes really helped clean up a messy garble of code and provide a more readable format than what we were using previously.