With such a confident title, you may be asking yourself, ‘does his PHP Standard Class really have what it takes to rule them all?’. Now let me assure you, I have no idea, I’m like the only person who actually uses the class. But perhaps if I dropped the class into the hands of a certain evil overlord… who knows?
What does this class do that is so great? Well a few things:
- Register and unregister options to both the class and individual objects (and use JSON file to define default options)
- Use as an array with member ‘Array’
- Automatically handle getter and setter methods that are able to traverse across multiple classes.
- Define ‘init’ functions for specific members that are run the first time you access a member.
I’ll tackle these one by one:
Registering and Unregistering Options
Why do this? Well, in re-used and extended classes, there is often a need to customize the behavior of the base class without overriding the base method. This could either be because you don’t want to have to create a class that extends the base class, or because you want to modify the behavior in the middle of the method. Also, there are times when it is important to be able to unregister your settings once you’ve finished using them. Additionally, the class will look for a file in the same directory as the source file named ‘ClassName.json’. If it finds such a file it will load it as the default options. This lets you separate out the static options of a class from the application code of the class.
Use as an array
The standard class contains a member ‘Array’. This member is exposed to implement array access and Iterator to allow you to treat the object as an array. Also, the constructor of the Standard Class allows you to pass in an array as the argument that it will use to to initialize the ‘Array’ member.
Automatic Getters and Setters
I find it tedious and unnecessary to manually define the getters and setters for each property of my class. In my view, getters and setters should only be defined if they are doing something special. So let it be so, Standard Class! Th class uses magic method __call to parse any undefined method beginning with ‘get’ or ‘set’ and automatically get or set the specified. It will first check if a member with the name is defined. Next it will check if there is an index in the ‘Array’ member.
Additionally, it uses a cool special syntax for traversing accross multiple classes to retrieve a member. For example, if you have a class named ‘Course’ that has a member named ‘Students’ that is an array of class ‘Student’ and class ‘Student’ contains a member ‘Address’ which is a class ‘Address’ and class ‘Address’ has a member ‘City’, you can use the following to retreive a list of all the cities the students in a course:
Another cool feature is the ability to define ‘init’ functions that will automatically be called the first time an undefined member is retreived. This allows you to post-pone initializing the member if and when the member is needed. This works by tapping into the magic method ‘__get’ which is called when an undefined member is accessed for a class. When this happens, it will check if there exists a method named ‘initMemberName’ and run that member.
This simple example uses a bunch of the features described above. The example defines a class that builds HTML pages that display information about tables in a MySQL database. In the example, we use options to customize the behavior of how the class behaves for a certain table:
The code is part of the Tfc Web Bundle repository ( ‘/BaseBundle/Core/StdClass.php’): https://github.com/ngagnon1/TfcWebBundle