Die Flagge des Marasek

Dekostreifen

English

Aktuell Texte Der Comic Impressum Kalender Suche PHP-Klassen Container-Wizard main.s21

Kategorien

Buch
Computer
Computerspiele
Film
Geschichte
Gesellschaft
Idee
Kunst
Natur
Persönlich
Politik
Programmieren
Religion & Philosophie
Weblog
Weltpolitik
Weltsicht
{{login}}

paran: Basic

Permalink
Vorheriger: Tatort (05.10.2008)Nächster: paran: EPDO
Eingeordnet in: 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:

<?php
$img
["src"] = "cats/cute/minky.jpg";
$img["border"] = 0;
echo 
Basic::xmltag("img"$img);
// 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:

<?php
$input
["type"] = "text";
$input["name"] = "surname";
$input["value"] = $name
if($disabled == TRUE) {
    
$input["checked"] = "checked";
}
echo 
Basic::xmltag("input"$input);

// the above construct surely beats this one:

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

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:

<?php
$a
["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.

construct

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.

<?php
$class 
"Date";
$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

Kommentieren

Bitte beachten: Kommentare sind nicht sofort sichtbar, sondern werden erst nach einer kurzen Prüfung freigegeben, sofern keine rechtliche Beanstandung vorliegt.
Rechtlich bedenkliche Inhalte werden entweder entschärft oder nicht veröffentlicht.

* Titel  
* Nickname  
* Kommentar