News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Jani Hartikainen's Blog:
Practical uses for reflection
February 17, 2009 @ 10:22:36

Reflection can be a handy tool when you need it, but how many times have you actually found a use for it in the past few applications you've written? Jani Hartikainen has one suggestion of a place it can be used - form generation.

Most web applications use forms. Forms often represent some model, such as a news post. If the model is simple, writing the form in HTML is not too bad, or we might be able to use a scaffolding feature in our framework. [...] Here's a good use for reflection! Since in models we often have a naming scheme for the data the users would be able to input, we can utilize this knowledge with reflection to generate a list of possible fields in the model, and then generate the form with less typing required from us.

His example looks into the model class and pulls out the "get" functions and, after pulling out the name, adds it to a fields array. This array is then passed out to the display part of the script and looped over to create a text field for each. He expands it a bit to also include checking for "@return" values in docblock comments for the type of form field that should be displayed.

1 comment voice your opinion now!
reflecton api form generation practical suggestion model view


blog comments powered by Disqus

Similar Posts

PHPBuilder.com: Taking Advantage of Simple Cloud with the Zend Cloud Component

CodingThis.com Blog: Quick XML-to-HTML Templating in PHP Using SimpleXML

Jani Hartikainen's Blog: Another idea for using models with forms

Jim Plush\'s Blog: The Event Driven Mobile Application on Minimo

SitePoint PHP Blog: Writing API Documentation with Slate


Community Events

Don't see your event here?
Let us know!


podcast community interview language extension unittest laravel development opinion library laravel5 framework series api wordpress introduction voicesoftheelephpant conference middleware release

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework