Conditional compilation

That’s one thing I am really missing in PHP – conditional compilation. Mostly what I need is conditional non-compilation, of course. Let’s see:

function dosomething($param) {
// now do something


Nothing unusual in this code, looks just fine – you want to know what happens in your functions, you log and maybe even profile entries and exits. However, there’s one small problem. I want these log functions out in production – they cost performance (function call is not very cheap in PHP) and anything useful I can do with them I could do in staging. Of course, I could just add an if($DEBUG_MODE) into functions and make them do nothing. But function call is still there. I could keep two versions – debug and production – but this would lead to a serious mess very soon. I could also surround each call with if($DEBUG_MODE) – but that would look horrible and would be very irritating to write – and on top of that let’s say I am a performance maniac – I don’t want even single extra if be there if it doesn’t have to be. And remember that since we have bytecode caches, the more we push from run-time to compile-time, the better!

So what I would really like to have is something like #ifdef in C – so that certain code is just ignored by the compiler, once certain conditions are met. I don’t really want full-blown C preprocessor – it allows you to do evil things. But something like small conditional compiling here and there won’t hurt.

BTW: if we’d peek outside PHP world, Java doesn’t have this capability too, but in Java you have the compiler, which can statically optimize the code and throw out the parts that are known not to execute, reaching the same goal, effectively. And it even has provision for that in the language definition. Even though I wouldn’t mind having that in Java too – and some people tend to agree with me and some even have more creative solutions. So, returning to PHP, maybe we would have use for something like that too?


5 thoughts on “Conditional compilation

  1. Same problem, but i resolved with dynamic include,
    function doLog($msg) {
    print $msg;

    function doLog($msg) {
    // nothing

    function LoadLogFile( ) {
    global $env; // better a constant or define

    $the_file = ‘file2.php’;

    if($env == ‘DEV’) {
    $the_file = ‘file1.php’;

    return include $the_file;


    $env = ‘DEV’;
    include utility.php

    function some_func($param) {
    doLog(“my msg”);

  2. I agree – although want it for a different purpose: building for specific PHP versions. Take the following function declaration:

    function funky(&$data = NULL) {

    …This causes a compile error on PHP4, but is just fine in PHP5. So obviously, I’d like to do something like

    #if PHP_VERSION = 4
    function funky(&$data) {
    function funky(&$data = NULL) {

    …sadly, this isn’t possible, meaning my function is doomed to always require a parameter, even if it’s just NULL, if I want this to work on PHP4 and PHP5. It is possible to use inline code to do the above, but that’s just an extra inline processing overhead that really shouldn’t be necessary.

  3. Same here. I want this so I can have stubs on some of my static classes that get included. Like APIs. I want to be able to replace an API class with a stub class while i’m testing.

Comments are closed.