Code generation in PHP
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 fragment 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.