Tuesday, March 2, 2010

Wrapping it up nicely :: Base Object :: Part 6

So, now we have the whole library. Now we need a name. I will call it dotPHP. Just because I like the name.

Now, why not even do the definition and extension in one line of code? I will define a DotPHP class to help us contain it all within a nice namespace:

class DotPHP {
        public static function defineClass($sClassName, $mBase=null) {
            eval('class '.$sClassName.' extends Object {}');
            if ($mBase) {
                $sClassName::extend($mBase);
            }
        }
    }

Now the all the code is done in a fewer lines of code:

DotPHP::defineClass('A', array(
        'method3' => function($owner) { print 'method3'; }
    ));
    DotPHP::defineClass('B', 'A');
    
    print $o2->method3(); // output: method3

Please leave some feedback so that I know if you guys like the syntax.

No comments: