Writing a Powerful PHP Property Manager Class
November 17, 2011
Posted by on
I’m going to recreate the process I went through in writing a side class that automates everything I could think of to intelligently support powerful business objects. I think it does an awesome job of balancing performance with “language conformity” — the idea that any specialized library or framework should not require you to configure anything to make it work. You shouldn’t have to “add this URL to that array” and “put the file in this particular directory”, etc., type of disjointedness. No black box stuff, although sometimes using PHP interceptor functions, along with inheritance, can seem like magic.
“The only way to get a practical and high performing product is to fully exploit the available technology.”
After working with PHP for about 6 months in a generic “framework” (It was homegrown, but just like other popular frameworks in its organization, it assumed a non-business aware stance. While I am 100% behind generic implementation, it is clear that this type of thinking as a system structure leads to a non-cohesive codebase), I decided as a way to learn OO PHP, I would try to write some classes that represented the business’ entities and hide a lot of the repeated data access in them.
Obviously I also wanted some way to only access the DB when necessary and to cache results during the server trip as object state. I also wanted centralized validation. Having each new method define its own validation rules, the way we were doing it, seemed insane to me. There set of types to validate was finite, why have to keep defining what a “retailer_id” must look like, right? And with the rules being repeated and scattered, inconsistencies were guaranteed.
As bonus goals, I wanted auto-complete functionality to be available in supporting IDEs and I wanted to be able to generate detailed class documentation that really gave a picture of the business domain.
What I’m going to try here was originally implemented within the base class of all the business classes. I realized to make it useful it needed to be in an auxiliary class, but had only partially implemented it that way (but with a lot of new and improved features like memcache and validation built in). It was originally written using PHP 5.1.something, I think, so there might be more refinements that would make the interface cleaner. I always kept the magic to a minimum and only used Reflection for initialization (unlike Yii, for instance, which relies heavily on it — I have to think that’s not cheap).