Code generation in PHP

<![CDATA[I've started working on a class-based code generation utility for PHP (in PHP). Like the scripting engine it’s working out simpler than I expected. Quite how powerful or indeed useful it could be is yet to be seen.

The way it works is by allowing you to define fragments of classes. For example the Singleton fragment is:

class !className {
    static private $instance;

    private function __construct() {
        // Code Start
        // Code End

    static public function Init() {
        if (!self::$instance) {
            self::$instance = new !className();
        return self::$instance;

You create a class with some number of fragments. Each successive fragments adds specified method and properties to the class. If two (or more) fragments define the same method (probably common with constructors for instance) the the code for the second fragment get’s added in the //Code Start ... //Code End sections.

Ultimately you will be able to change your model (defined in XML and it will update your code accordingly, maintaining all the code within the //Code Start ... //Code End sections.

PHP, code, code generation, programming, PHP5, OOP]]>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s