Posts Tagged ‘meta-reflection’

Meta-Reflection variable name detection problem for Pull Widgets in PHP

Written by AbiusX on . Posted in Computer, Development, English, Software Engineering

First of all, Happy Nowruz! Tomorrow is officially the new Jalali year's start. Best of wishes to everyone.

The Problem

I've been working on an elegant design for a new PHP Widget library for some time now. It is intended to provide a Pull MVC feature for jframework. As you know, MVC provides a separation of concerns, allowing different expertise people to work separately on their designated part of the application. The model part (which is the business logic of the application, plus some of the solution domain) is usually very re-usable and employs object oriented to great extent. The controller part, is mostly lightweight and consists of code usually not re-usable.

The problem resides in the view part of the MVC. Views are mostly HTML/CSS that dump some variables and arrays. Sometimes templates are employed in views, to reduce repeated code, but still most of the views are repeated code. Imagine two different areas of a web application, both providing tabular data of different origin. 70% of their code is the same but due to difference of data nature, usually no re-using is done.

I'm not gonna talk about benefits of code re-use here, but you get the picture.

Pull MVC, in contrast to Push MVC, is a system in which the view asks for content to render themselves by pulling them in, instead of pushing the content directly on the screen. It is usually provided by widget systems, and is much more object oriented. The problem with Pull systems is that view is usually designed and not developed, by a graphical designer who is adept in HTML and CSS.

The Actual Thingie

The first thing I wanted to do, was to preserve maximum simplicity. You can not expect a library to require redundancy to provide code re-use. I do not like libraries that take control of everything and don't let developers to change their mechanics. So I wanted to replace this practice:

$form1 = new jForm();
$button1 = new jFormButton("form1");
$button1->setLabel("Push Me!");

To a single liner elegant approach:

$form1 = new jForm(jForm::Method_Post);
$button1 = new jFormButton($form1, "Push Me!");

So the first thing I needed to be done, was for jForm (or any other jWidget instance) to know what variable it is assigned to - in this case $form1 - and name itself after it. The name is actually used a lot in the generated HTML code, for validation, for CSRF protection, for HTML form element names, for HTML IDs, for CSS classes and a lot more.