Die Flagge des Marasek



Current Texts Comic Imprint Calendar Search PHP-Classes Container-Wizard main.s21


Class Library
Global Politics
World Outlook

paran: Basic

Previous: Tatort (05.10.2008)Next: paran: EPDO
Assigned keywords: Programmieren

paran is not going to work without Basic. Basic is simple, small and absolutely central. When I started to work for another company, which had their library in place already, I made it a condition that I might at least use some components of Basic lest to loose the last vestiges of my sanity.

Basic is just a collection of static methods which I turned into a class because I did not want to have a single include("functions.php") around. The central method is Basic::xmltag(), which you can search within the library and will find a lot. It will turn a string (tag name) and an array (attributes) into an XML opening tag:

["src"] = "cats/cute/minky.jpg";
$img["border"] = 0;
// echoes <img src="cats/cute/minky.jpg" border="0" />

This definitely beats the usual mess of slashes and strings when people concatenate XML-Tags by hand is certainly more elegant when having conditions:

["type"] = "text";
$input["name"] = "surname";
$input["value"] = $name
if($disabled == TRUE) {
$input["checked"] = "checked";

// the above construct surely beats this one:

echo "<input type=\"text\" name=\"surname\" value=\"".$surname."\" ";
$disabled == TRUE) {
" />";

The latter is horrid, not to mention that it calls for a security issue if $name is not properly sanitized using htmlentities(). xmltag will properly mask all values. Along that line come two handy relatives, Basic::xmltagEnclose and Basic::xmltagUnsafeEnclose:

["href"] = "http://www.hadanite-marasek.de/";
$a["class"] = "extlink";
// & gets masked to &amp;
echo Basic::xmlEnclose("a"$a"Marasek's page & blog");

// to prevent double masking of htmlentities, use unsafe enclose. You then have to care about
// proper masking yourself.
echo Basic::xmlUnsafeEnclose("td", array(), Basic::xmlEnclose("a"$a"Marasek's page"));

I have been criticized for using such a simple concept as opposed to DOM or similar, more object oriented mechanisms. Well, first of all, I know how to handle DOM. However, it should be clear that a method which keeps a representation of the entire DOM tree in memory and where every element is an object is a little bit more complex and has more overhead. xmltag served me very well so far.

dump and var_dump

Take me as a fan of "echo debugging". Some people scoff at this, but I'll usually get a bug faster with a few echoes than firing up all this debugging stuff which is not very trivial when you want to debug on a remote server and so on. Basic::dump and Basic::var_dump just add <pre></pre> around calls of print_r and var_dump - wonder why the PHP guys didn't put that as an option.


I never liked the way of dynamically constructing objects with new $object(), as it looked as primitive as $function(), which I had long before replaced by call_user_func_array, first alone and then within paran: Callback. construct builds objects, internally using Reflection.

$instance = new $class(200811);
// nicer, clearer:
$instance Basic::construct($class, array(200811));

The latter does not just feel "nicer", it also looks nicer, allows for dynamic parameters and is easier to identify (globally search for Basic::construct and you get all runtime instantiations in your project).

Dieser Text ist Teil der Serie Paran in Action

paran: Autoloader
paran: Callback
paran: Basic
paran: EPDO


Please note: comments posted won't be visible immediately, as they will be checked for harmful content.

* Title  
* Nickname  
* Comment