18

I would like to override, let's say mysql_num_rows with let's say following:

$dataset = array(array('id' => 1, 'name' => 'Zlatan', 'onSOF' => 1), array('id' => 1, 'name' => 'Guest', 'onSOF' => 0));

function mysql_num_rows($dataset) {
    return sizeof($dataset);
}

Does PHP support built-in function overriding?


EXTENDING

I want to create an OpenSource solution which will override all existing mysql_* functions, and it their function body I'll be using PDO instances and methods, and properties.

This means that users who already use the mysql_* and find it hard to move completely to PDO, should just include this function override, and all properties, function calls, function return values, argument values, etc, should be left the same.

2 Answers2

22

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

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
  • yes, this might work, but every user must have http://www.php.net/manual/en/book.apd.php installed on his/hers server... –  Mar 05 '13 at 18:36
  • @Zlatan True, alternatives are namespaces like they explain [here](http://stackoverflow.com/questions/3927995/override-default-php-function). Namespaces only work in PHP 5.3 and later. – Ron van der Heijden Mar 06 '13 at 07:43
  • great! so that means I can name a namespace on top of my script, let's say: `"mysql2pdo"`, and override all the `mysql_*` function on the fly throughout that script? –  Mar 06 '13 at 09:17
  • yes, but how would you import a PDO class within that namespace? :) –  Mar 06 '13 at 09:48
  • @ZlatanO. example `use Symfony\Component\HttpFoundation\LaravelResponse as FoundationResponse;` that would be the way using another class in that namespace – Alex Mar 06 '13 at 09:52
2

Install runkit & use runkit_function_redefine. Only do it on development/testservers, never in production.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • I've extended my question, please re-view it :) –  Mar 05 '13 at 18:14
  • @Zlatan: in that case... nope, no can do, unless they just haven't `mysql` installed. – Wrikken Mar 05 '13 at 18:18
  • 3
    @KamilDziedzic: because it causes lousy performance issues, will send developers on wild goose chases, and shouldn't be needed (it's usually just the lazy man's way out of refactoring). – Wrikken Mar 05 '13 at 18:20
  • 1
    @Wrikken, Lazy? It's economical. – Pacerier Oct 20 '14 at 16:19
  • 1
    @Pacerier: it seldom is. Some minutes saved here, are costing you a LOT in performance, and a LOT in debugging hours later. But if you like to runkit-redefine on your production servers I'm not going to stop you, I'll only make damn sure I'll never hire you :P – Wrikken Oct 20 '14 at 18:01
  • @Wrikken, Depends on your use case. – Pacerier Oct 20 '14 at 18:11