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.

3
  • Method overloading? Yes. Function overloading? No.
    – John Conde
    Mar 5, 2013 at 18:02
  • 1
    You could try using namespaces to use the same function names but keep the functions separate. Mar 5, 2013 at 18:03
  • Just if(count($rows) > 3) or whatever.
    – Xeoncross
    Mar 5, 2013 at 18:04

2 Answers 2

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

5
  • yes, this might work, but every user must have php.net/manual/en/book.apd.php installed on his/hers server...
    – user1386320
    Mar 5, 2013 at 18:36
  • @Zlatan True, alternatives are namespaces like they explain here. Namespaces only work in PHP 5.3 and later. Mar 6, 2013 at 7: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?
    – user1386320
    Mar 6, 2013 at 9:17
  • yes, but how would you import a PDO class within that namespace? :)
    – user1386320
    Mar 6, 2013 at 9:48
  • @ZlatanO. example use Symfony\Component\HttpFoundation\LaravelResponse as FoundationResponse; that would be the way using another class in that namespace
    – Alex
    Mar 6, 2013 at 9:52
2

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

6
  • I've extended my question, please re-view it :)
    – user1386320
    Mar 5, 2013 at 18:14
  • @Zlatan: in that case... nope, no can do, unless they just haven't mysql installed.
    – Wrikken
    Mar 5, 2013 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 5, 2013 at 18:20
  • 1
    @Wrikken, Lazy? It's economical.
    – Pacerier
    Oct 20, 2014 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, 2014 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy