Automatic PHP Code Generation with Memio

Originally published at: http://www.sitepoint.com/automatic-php-code-generation-memio/

Ever thought of writing code responsible for generating certain PHP classes, methods, properties automatically? Read on to get the details on when exactly automatic code generation may be helpful and – what’s most important – how to implement it properly using the Memio library.

The Concept

The basic idea is quite simple. You write code which will create other parts of the code, functions, variables, classes, doc blocks, etc. As you do it in a programming language (PHP in our case), you can specify parameters, if-else statements, loops, and so on.

Of course, being able to create PHP code automatically doesn’t mean we, the developers, will be replaced. But it may be used to form a basic structure which will later be further developed by a human. For example, instead of copy-pasting to prepare an initial set of classes in your application, you can use a generator.

Code generation is already being used in various frameworks. See Symfony2 GeneratorBundle, CakePHP console commands or Laravel Artisan for examples.

Continue reading this article on SitePoint

Code generation can be really useful. I used to use a framework called QCodo that was a code generation framework. The framework was really well written, and I used it for several projects. Unfortunately the framework developer let the framework go. A group of developers forked the project into QCubed, but it really wasn’t the same without Mike Ho.

Word of caution regarding libraries like this and APC cache – they can easily end up building a code file larger than the code cache allows for, particularly when building dependency injection classes. Granted, you need a very large project to be facing this issue, but it’s not inconceivable.

1 Like

Hi,
is there a situation where Memio really shines? I got the impression (based on the article and docs) that it is just another code generation library, nothing exciting.

You don’t need to write the target code in strings, work on joining string variables, etc.

Can the setBody call be “worked around”? Can I actually generate the body code?

What other ones do you know of that are good and in sync with modern PHP?

Worked around to do what?

What other ones do you know of that are good and in sync with modern PHP?

From memory I know of zendframework/zend-code and nette/php-generator.

Worked around to do what?

The article states that I would not need to write target code in strings. On the contrary - compete function body seems to be a string variable. Maybe a I could have asked the question another way: is there an alternative way of setting method body (preferably in compliance with the statement about not needing string variables)?

Hmm, I see. I don’t think so. How would you propose that be done? I can’t think of a reliable one-size-fits-all solution.

That is exactly the information I expected to find out in the article :slight_smile: I can imagine a template method is created beforehand, it’s body parsed (nikic/php-parser may help with that), the AST transformed and assembled again. Also a (very) complex AST builder is possible (yet probably impractical).

Interesting. Might be a good project to adopt…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.