Maintaining Orthogonality In Php

PhpLanguage has many odd language features which end up introducing odd namespaces and global state couplings. This is a topic I've been thinking about for some time, and what I'd like to evolve here is strategies or mini-patterns or idioms for maintaining orthogonality in PHP.

Use of autoglobals:

PHP has $_REQUEST[], $_POST[], $_GET[], $_SERVER[] and $_SESSION[] autoglobals.

To maintain orthogonality:

First strategy (make unglobal):

Second strategy (encapsulate with local object):

Third strategy (define where to access):

In other words, top-level .php scripts are ModelViewController controllers.

Program output:

PHP has:

It may be thought of in this way: PHP has two methods for returning textual data. Via "return" and via "echo." Using "echo" can be caught using ob_start(), ob_end().

To maintain orthogonality:

Alternately:


Regarding autoglobals:

You are welcome to wrap them behind functions or objects, but I've never encountered problems with these being global. If you have seen specific problems with it, I would like to study the scenarios. KISS wins out I think. Layers and layers of stuff "just in case" can make for verbose, hard-to-read code. (PrematureAbstraction). Generally if you have something fairly complicated to track the state of, the "PHP way" is to put it in the database. Loading up the SESSION array with lots of stuff is a smell in my opinion.

Let me relate the experiences that have caused me to conclude this:

I've just added two more strategies. I'd be interested to read your comments.

I guess I am not a believer in ModelViewController. That may be the difference.


CategoryPhp


EditText of this page (last edited February 23, 2006) or FindPage with title or text search