Tag PHP class for dynamically generating HTML

This is a utility class which I’ve used in a basic form for a while now to quickly generate HTML programmatically in PHP.

The idea behind it is that I wanted a fairly simple object that would represent an HTML tag element.  Once created, the content and attributes can be changed dynamically and when ready, a render() method can be called to output the HTML.

I know there are loads of templating classes out there and you could use the built in PHP XML functions to do this, but they are overkill for what I want. This is particularly useful for rapid prototyping and testing. You can quickly create HTML output without having to actually write any HTML. Plus this is an interesting programming exercise.

So far, it’s pretty simple:

$tag = new Tag( 'div', ['class' => 'my_class'], 'my content' );

$tag->render(); // outputs <div class="my_class">my content</>

$tag->setAttribute('class' => 'new_class');

$tag->render(); // outputs <div class="new_class">my content</>

The next step is to make it recursive and then add some kind of find/ search method to target embedded Tags.

The project is here: https://bitbucket.org/dgifford/tag

Installing PHP7 mcrypt on Debian

I’ve run into this problem a few times when setting up development stacks for Laravel, OpenCart and some other frameworks – they require the mCrypt PHP extension, but it is not included in Debian 8.

After a few failed attempts and some Googling, this seems to work for me:

apt-get update
apt-get install mcrypt php7.0-mcrypt
apt-get upgrade
service apache2 restart

Of course, you need to be the root user to do this.