PHP

PHP code generator online

<![CDATA[I've just finished a very early version of the PHP code generator I mentioned a while ago. The code for it is rather messy at the moment so I'm not releasing it, but a web form is available to demonstrate it:

PHP Code Generator

At a simple level it takes class fragments you define and includes them in the file. The only fragment defined at the moment is Singleton. To include it you would use:

//cg fragment=Singleton&className=MyClass

Running the generator on just that basically just returns the Singleton fragment with the specified class name. The clever part is it correctly assembles code based on multiple fragments as well as using the code in the file itself. This means if you define MyClass in the file and give it some other methods, these will remain in the generated code.

You can also add code to methods defined in fragments and they still remain, providing you follow a few rules. Only certain fragments will allow code to be added to their methods – they will have //Code Start and //Code End lines. Anything you add between these will remain – all other code will be stripped when the code regenerated.

Issues

Any comments between methods will be removed – with the exception of docblock comments. Comments inside methods are treated like any normal line – they must be inside //Code Start ... //Code End.

At the moment support of visibility keywords and static is a little dodgy. The method should have the properties in it’s first definition.

A methods parameters however will be the same as the last definition. For example the Singleton defines a constructor with no parameters. If you then give it parameters it will keep them.

At the moment the opening brace for a method must be on the same line as the function declaration (i.e. it doesn’t support “one true brace”). This should be easy to fix, but it means methods must be defined like:

function myFunction() {
}

A little bit of cleverness

Although I’m not going to reveal exactly how just yet, there is a small bit of markup supported in the fragments allowing bits to be dynamically defined. To see this generate a Singleton and then add a parameter to it’s constructor (and regenerate). The Init method magically has the same parameters.

Finally…

This is very early and very untested with regards to complicated scripts. Any odd behaviour, feel free to let me know. 🙂

PHP5, OOP, code generation]]>

Advertisements

3 thoughts on “PHP code generator online

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s