Writing PHP the European way

I read about a rather brilliant PHP function today, one called __halt_compiler(). By brilliant I of course mean capable of causing all kinds of havoc.

What it does is cause the PHP compiler to ignore everything after the function call, and define a constant __COMPILER_HALT_OFFSET__ that signifies the position in the current PHP file where the function was executed.

How can you use it, you ask? Here’s an example. There’s been discussion what it would be like if PHP were British, but see how easy it would be to make it European:

<?php
//
// How to code PHP the European (or Finnish) way
//

$fp = fopen(__FILE__, 'r');
fseek($fp, __COMPILER_HALT_OFFSET__);
eval(preg_replace('/(€|FIM)/', '$', stream_get_contents($fp)));
__halt_compiler();

// From now on we can use € or FIM instead of dollars!

for(€i = 0; €i < 10; €i++) {
        echo "Hello FIMi!\n";
}

There are of course valid uses for the function as well, such as packaging code and binary in one file for system installations or similar. On the other hand, since PHP 5.3 we can package everything with Phar, which is basically Jar for PHP. But it won’t let you write in €uropean… ;)

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>