I’ve recently been playing around with some of PHP’s magic methods, they’ve been one of those things I’ve been meaning to read up on properly since PHP 5 was launched.
Magic methods are a set of methods that can be declared within a class in PHP, which are automatically called under certain circumstances by PHP. They’re recognisable by their prefixof two underscores (the one most often seen is probably __construct, the PHP 5 way of declaring an object’s constructor).
One of the more interesting methods is named __call. A call to this method is triggered whenever a non-existent or inaccessible method of an object is called.
I’ve used this to put together a quick way of generating little fragments of HTML, mostly as an exercise in getting my head around how these methods work, and to save myself some time in quoting/escapeing/merging strings to form HTML from within PHP (download available here: HtmlGen.php)
A single object is initially created, using:
$h = new HtmlGen();
Any methods called on this object are then passed throug the __call() method, and assumed to be requests to generate HTML tags of the same name. Any method call with an underscore followed by a word is assumed to be an alternate way of rendering that tag.
Any array argument is assumed to a an associative array of attributes, anything else is assumed to be content to be put within the HTML tags.
Each method call returns a copy of the original object, which allows chains of methods to be called. When needed to be rendered, the object’s __toString() method takes care of turning the object into an HTML tag.
Some basic examples of usage along with the HTML printed:
echo $h->p(); -> <p></p>
echo $h->p; -> <p></p>
echo $h->somemadeuptag; -> <somemadeuptag></somemadeuptag>
echo $h->p(‘Some text’); -> <p>Some text</p>
echo $h->div(‘Foo’, ‘Bar’, ‘Baz’); -> <div>FooBarBaz</div>
echo $h->p(array(‘class’=>’foo’), ‘Text’); -><p class=”foo”>Text</p>
echo $h->p->div(array(‘class’=>’main’), ‘Text’); -> <p><div class=”main”>Text</div></p>
echo $h->ul($h->li(’1st’), $h->li(’2nd’)); -> <ul><li>1st</li><li>2nd</li></ul>
More interesting is the ability to escape strings, and to provide shortcuts for common HTML generating operations:
$h->escape_html = true;
$h->escape_attributes = true;
echo $h->div(array(‘style’ => ‘” onclick=”alert()’))->p(‘<script>some bad stuff</script>’); ->
<div style=”" onclick="alert()”><p><script>some bad stuff</script></p></div>
And some shortcuts for common operations:
echo $h->ul->li_list(’1st’, ’2nd’, ’3rd’); -> <ul><li>1st</li><li>2nd</li><li>3rd</li></ul>
echo $h->h2_short; -> <h2/>
echo $h->h2_long; -> <h2></h2>
And a final example to print out three paragraphs each containing a list:
array(‘class’ => ‘section’),
<p class=”section”><ol><li>a</li><li>b</li><li>c</li></ol></p><p class=”section”><ol><li>1</li><li>2</li><li>3</li></ol></p><p class=”section”><ol><li>foo</li><li>bar</li><li>baz</li></ol></p>
It was put together on a Friday afternoon, so probably needs some work (some global option for formatting the output with linebreaks and spaces would be nice), but hopefully serves as an interesting demo for what some of PHP’s magic methods can be used for.
Library download here: HtmlGen.php