Cancelling Native Function

<?php

namespace A {
    function print_r($x) {
        print 'No print_r for you!'.PHP_EOL;
    }
}

namespace A {
    $x = [];
    print_r($x);
}

// It looks like it, but
// A\B is not a child of A!!
namespace A\B {
    // Just print_r for you, with fallback
    print_r($x);
}

?>

PHP native functions are part of the global scope. In a custom namespace, the local definition of a function has priority. And when this fails, PHP fallbacks to the global space. This is a backward compatibility process, that prevents developer from adding ALL native PHP functions as a use expression in every file. This would be long, boring and a performance boost.

To cancel a PHP native function, such as print_r or var_dump, the trick is to create a definition of that function in the same namespace. It is then used with precedence, and may be prevented from having any impact.

This would be different from using the disable_functions: then, the function undefined, and shall be defined again, in the global namespace, to be properly neutralized.

This trick does not work with classes.

See Also

PHP Features

Last updated: 14 July 2026