How to override built-in PHP function(s)?

I think it could be done like so:

//First rename existing function
rename_function('strlen', 'new_strlen');
//Override function with another
override_function('strlen', '$string', 'return override_strlen($string);');

//Create the other function
function override_strlen($string){
        return new_strlen($string);  
}

found it here

Notice that every host must have http://php.net/manual/en/book.apd.php installed on the server.

Edit

Another way is to use namespaces

<?php
    namespace mysql2pdo;
    use PDO;
    function mysql_connect() {
       return new PDO();
    }
    echo mysql_connect(); // Causes error because we don't have the parameters
?>

Test it here

Leave a Comment