2

Is there a way using function_exists that I could check that if for example if 'mysql_query' is called and therefore I can run some code via the if statement? to basically error it so that I can go and change it to PDO?

3
  • 3
    Maybe what you want is to create a wrapper function for mysql_query that implements you if logic? Commented Nov 21, 2015 at 22:16
  • If you're interested in finding out where mysq_*() functions are hiding in your code, You might use a find/grep similar to this: find /path/to/your/app -name "*.php" -exec egrep -Hn "mysql_\w+" {} \; if you have access to a unix-like OS. Commented Nov 21, 2015 at 22:35
  • 3v4l.org/vEldT Commented Nov 21, 2015 at 22:42

2 Answers 2

1

function_exists() is only for checking if a function is defined rather than if a function is being called.

However you can rename and override a PHP function to get the desired effect, using a wrapper as @wogsland mentioned in the comments. This method requires APD installed. For example:

<?php

// First rename existing function
rename_function('mysql_query', 'original_mysql_query');

// Override function with another
override_function('mysql_query', '$query', 'return override_mysql_query($query);');

// Create the other function
function override_mysql_query($query)
{
    echo "Calling original_mysql_query($query)";
    return original_mysql_query($query);
}
Sign up to request clarification or add additional context in comments.

Comments

1

In php 7, mysql_* functions are deleted and you are then able to redefine them pointing them to their new functions, such as mysqli or PDO.

But to answer your question, you could also rename the internal functions in PHP explained in this page: http://php.net/manual/en/function.runkit-function-rename.php

And create a user function with the same name as the function you're trying to call and let it do something else instead. But I would not recommend it.

As NoChecksum also stated, "rename_function" and "override_function" is also an option.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.