Today was held the first online meeting of the Arab PHP Groups, The meeting discussed many topics about the groups, and every group gived a report about its activity since the launch of the online community. During the meeting I given a speech about Code Generators, so if you missed the event and you are interested to hear about what have been said this is a copy my speech, It took about 30 mins, not about but exactly !


[22:00] my name is Hatem Yacoub from Tunisia, PHP developer since 1998 and author of many open source projects such MySQL Backup Pro, PHP Voice and VoiceXML PHP Framework …
[22:01] also author phpmagazine.net and phptunisie the Tunisian PHP community, which is absent today in the meeting. But we’ll talk about it another time.
[22:02] to start the speech, PHP developers in their everyday work face many problems
[22:03] 1- Coding time (not code time), which the time taken to write a PHP application
[22:03] 2- Repetitive tasks
[22:04] 3- Increasing demand of web solutions (if you made an excellent website today, a new client will ask you for a similar one)
[22:04] 4- Cost of developers and development
[22:04] And as a PHP developer you are asked to find solutions
[22:05] that your boss won’t give you, but you have to find by yourself.
[22:05] What’s the topic today ?
[22:05] …
[22:06] I’m going to talk about Code generations and the different solution that it might give to real world applications
[22:06] Even with the small theory that is needed here.
[22:07] Code generation is simply a technique that developers use to generate automaticly a code.
[22:08] whatever this code is : classes, documentation, full web application, website …
[22:09] Code generators are so application that use to generate code
[22:09] There is many different types of code generators such Code Munger, Partial Class Generator and Tier Generator
[22:10] I just took these few examples to develop and explain more clearly the aspect of some code generators
[22:10] 1- Code munger
[22:11] which is the most common type that I used to face very frequently
[22:11] The idea is simply to process one or more files to generate output files.
[22:11] It’s used for example to generate documentation
[22:12] and there is few projects that you may know such PHPDoc, JavaDoc, PHPDocumentator …
[22:12] 2- Partial Class Generators
[22:12] which is a technique to Generate sets of base classes
[22:14] This is very helpful for example to generate an outline, that the developer will just fill with the code he want
[22:14] just like the filling the blanks in english exams
[22:14] For example : Codegen, Php Object Generator …
[22:14] 3- Tier Generators
[22:14] Which builds and maintains an entire tier within an application
[22:15] Tier Generators usually used to generate entire web application
[22:15] and much more
[22:15] more complicated but offer for advanced users more freedom and certainly used in big projects
[22:16] For example : dia2postgres, MetaL, MetaStorage, uml2php, CodeCharge…
[22:16] enough definition ?
[22:16] I just prepared two examples to discuss today
[22:17] The first one is the simple steps to generate web application backend
[22:17] which is the Tier Generators model
[22:18] The technique is very simple if you are interested to get started with code generators
[22:18] First create your application’s tables (let’s say : books, authors, reviews, comments …)
[22:18] Write a management class for the first table (books)
[22:19] including methods : Browse, addNew, Edit, Delete, Search, and a switchOp…
[22:19] Then write a function that connect to the database and retrieve table informations
[22:19] After it try to replace field names and table names in the class you’ve written with the retrieved ones from database.
[22:19] Result you should be able to generate the same class with your new function
[22:19] Now it’s possible to use the same class with the different other tables
[22:19] …
[22:19] maybe it’s a little complicated though !
[22:20] hopefuly there is a second example
[22:20] While developing web applications it’s interested to work on localisation
[22:21] and make it easily to translate and offer more languages with it
[22:21] The second code generator is a Code munger
[22:22] which simply parse PHP scripts looking for word to translate, then generate a default locale file
[22:23] I have written a small localisation class that you can find here http://phpmagazine.net/download/gentrans/locale.class.txt
[22:23] the principe is to simply use something like locale::tr(‘hello’);
[22:23] inside your code
[22:23] inspite of using “hello”
[22:24] then the generator : http://phpmagazine.net/download/gentrans/gentrans.txt
[22:24] will walk through the code
[22:25] and look for anything to translate (locale::tr(‘something’))
[22:25] and generate the default locale file
[22:25] then the translation will be very easy
[22:26] before to finalize
[22:26] I want to talk about a small personal experience
[22:26] which might be helpful also
[22:26] I used to develop the PHP Voice project : VoiceXML, SSML, CCXML, N-GRAM and SRGS php classes
[22:27] lot of code
[22:27] difficult to maintain
[22:27] and sinceraly very boring
[22:27] for example VoiceXML Class is 2136 lines including documentation
[22:28] and the documentation specially when we talk about classes
[22:28] The solution was certainly code generation, you might guess ?
[22:28] Parsing standard DTD of VoiceXML, SSML, …
[22:28] Generate PHP Voice Classes
[22:28] More easy to maintain
[22:28] (1 generator inspite of 10 classes )
[22:29] Development is focused on the quality and not the quantity
[22:29] and finally a Record time to update the project
[22:29] This have been said
[22:29] I want to thank you for your attention
[22:29] and be sorry if I was long !
[22:30] you might find more ressources about code generation in www.codegeneration.net, info about PHP Voice at voicexml.phpmagazine.net and finally the demo code just here http://phpmagazine.net/download/gentrans/