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?

- 8,345
- 27
- 100
- 170
-
3Maybe what you want is to create a wrapper function for `mysql_query` that implements you if logic? – wogsland Nov 21 '15 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. – Michael Berkowski Nov 21 '15 at 22:35
-
https://3v4l.org/vEldT – Rizier123 Nov 21 '15 at 22:42
2 Answers
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);
}

- 1
- 1

- 1,206
- 1
- 14
- 31
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.

- 7,878
- 1
- 27
- 38