Page 1 of 1

Metadata and generation

Posted: Fri Mar 31, 2006 1:54 pm
by Gambler
Are there any PHP apps that work with metadata without any code/html generation? For example, RoR uses database schema as metadata and generates HTML and code. I'm interested in this approach, but I despise code generation (unless it's run-time). So I tried to write my own class that would handle database interaction, HTML form generation, input validation, etc, using single source of metadata, which is coded in PHP.

It's quite difficul to conceptualize. So far I thought about creating Field class, and extending it to simulate various kinds of fields.

Code: Select all

<? new Field_Line(1, 30, '/^[\w\d ]*$/'); ?>
The code above would create an object, which represents text line. 1 is minimum length, 30 is maximim length, it must match given regExp, etc. In theory, this information could be used to create database field (varchar(30)), create html field (<input type="text" name="myname" maxlength="30" />) and to validate input.

The problem is that fields should be held in some kind of container. Let's call it Record. It's hard to decide what should be done inside Field, what should be done inside Record, and what whould be done somewhere else. So I want to look how existing application deal with such kind of things.

Posted: Fri Mar 31, 2006 3:29 pm
by Christopher
I am someone who likes code generation, so I'm not sure I can be of much help. However, I use what may be a similar concept in some of my controllers, most commonly form controllers. I create an object that encapsulates the information about a parameter/column including the value, parameter/column names, filters, validation, whether it is to be persisted, code that should run after load or before save, etc. It gets passed by the controller through each step and finally to the view.

Posted: Mon Apr 03, 2006 8:50 am
by Maugrim_The_Reaper
RoR is something of a bad example...well, kind of. Depends on your perception.

With RoR and a few PHP ActiveRecord implementations, the metadata is gathered at runtime. Now that's really cool for cutting down your number of classes, but the metadata DB request, and subsequent class generation imposes a performance hit.

Hence, code generation across an application prior to a release (i.e. distributing pre-generated static classes) evades that cost with little loss of flexibility.

Personally it's a no brainer - but then that's my opinion. Look at both from your own perspective and things could look different - me, I'm stuck with a shared host for personal work which has no opcode cache ;). For the moment at least...