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. Commented Mar 5, 2013 at 18:02
  • 1
    You could try using namespaces to use the same function names but keep the functions separate. Commented Mar 5, 2013 at 18:03
  • Just if(count($rows) > 3) or whatever. Commented 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

Sign up to request clarification or add additional context in comments.

5 Comments

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

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

6 Comments

I've extended my question, please re-view it :)
@Zlatan: in that case... nope, no can do, unless they just haven't mysql installed.
@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, Lazy? It's economical.
@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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.