One PHP Standard Class to rule them all

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:

  1. Register and unregister options to both the class and individual objects (and use JSON file to define default options)
  2. Use as an array with member ‘Array’
  3. Automatically handle getter and setter methods that are able to traverse across multiple classes.
  4. 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:

$course = Course::get( "Biology" );
$student_names = $course->getStudents__Address__City();

Init functions

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.

An example:

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:

class PageMaker extends Core\StdClass {

  //Default static options for the class
  public static $DefaultStaticOptions = array(
    "Title" => "->getTableName", //states that method 'getTableName' should be used to retreive option 'Title'
    "Fields" => "->getFields", //states that method 'getTableName' should be used to retreive option 'Title'

  // 'init' method is called the first time an undefined member is accessed
  // This is used to only evaulate the member when needed
  public function initFields(){
    $this->Fields = array();
    $q = new Core\Query( "SHOW FIELDS IN `{$this->getTableName()}`" );
    $rows = $q->getRows();
    foreach( $rows as $r ){
      $this->Fields[] = $r;

  public function createPage(){

    $fields = "";
    foreach( $this->getOption("Fields") as $f ){
      $fields .= "<li>{$f['Field']} - {$f['Type']}</li>";

    $page = "
    return $page;

$pageMaker = new PageMaker(["TableName" => "Product"]);
$op_key = $pageMaker->registerOptions([
  "FieldsFilter" => function($item){ //'OptionName'+'Filter' defines a method that the option should be filtered through before returning
    foreach( $item as $ik => $iv ){
      if( $iv['Field'] == "DateTimeStamp" ){
    return $item;
  "Title" => "The 'Product' Table",
echo $pageMaker->createPage();

The Code

The code is part of the Tfc Web Bundle repository ( ‘/BaseBundle/Core/StdClass.php’): https://github.com/ngagnon1/TfcWebBundle


Leave a Reply

Your email address will not be published. Required fields are marked *