Using function_exists to check if a PHP function is found - php

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?

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);
}

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.

Related

Do PHP variable functions work in PHP4?

I know you can do something like this in PHP5:
function sayHi() {
echo "Hi!";
}
$func = "sayHi";
$func();
It is called a variable function in the documentation. But the docs don't say anything about what versions of PHP this works on. This could mean it works on ALL versions of PHP, but I doubt it. Specifically, does this work on PHP4?
For php 4.3 they are working. Build-in function test here, your source test here.
Definitely Yes. As you can see in here when there is a dependency in a certain function they set it below the title.
No dependcy in version of php
VARIABLE Functions
With dependency in version of php
INTVAL Function
note: so whenever you have concerns on a certain function that you will use regards on your php version better consult php.net for info.

it is possible to extend a php function?

My question is if it's possible to extend a declared function.
I want to extend mysql_function to add mysql query that insert into a table some logs : 'query' - the parameter of mysql_query, date,page...etc
My question is if it's possible to extend a declared function.
No.
You can extend a class method and call parent::methodname() to run the previous code (which is almost what you ask for), but for normal functions, there is no way to do this.
There are some esoteric PHP extensions that allow overriding functions, but I assume that's not what you need and their use is rarely practical.
What you probably want to do is create a new function, and call the existing function in it.
No, you cannot do that. Either enable the MySql Query Logs or wrap the code doing the queries into a Logging Decorator or use an abstraction like Zend_Db that can take a Profiler or use a transparent logging plugin for mysqlnd
You need to write a function that will take your query, log the sql first, runs your query, then return the results.
E.G
<?php
function mysql_query_log($sql)
{
mysql_query('insert into .... values ...');
$r = mysql_query($sql);
$results;
//do the normal thing you do with mysql here
return $results;
}
This is not extending a function though, you can only extend a class
It's not possible.
You should have created your own API (or use an existing one) to access the DB so when you need logging you can simply enhance your own API function. It also comes very handy if you need some custom error handling function. Refactor the code.
Well.. PHP says this: http://php.net/manual/en/function.override-function.php
from http://php.net/manual/en/function.rename-function.php
bool rename_function ( string $original_name , string $new_name )
Renames a orig_name to new_name in the global function table. Useful
for temporarily overriding built-in functions.
I believe that if you rename the original to original_mysql_query, then add your replacement function which does your logging and then calls original_mysql_query etc, that you will achieve your goal, assuming that you have the way to inject the rename on every page that will call MySQL_query. Most large sites have common code that is included at the top of every page that could do that for you.
There is also a built in php function called override_function (mentioned by ChrisH). It is not fully documented in the php man page but the user comments below the doc give you the information that you need to use it if you prefer it to the rename_function function. There was a discussion about being limited to one override if you needed to call the original function from the replacement. Using the rename_function instead of the override function eliminates that potential restriction.

Does function definition order matter?

In the script below, does the order in which items are declared matter?
For example, if the add_action points to a function that has not yet been defined? Does it matter or should the function declaration always precede any code in which its called?
add_action('load-categories.php', 'my_admin_init');
function my_admin_init(){
//do something
}
That doesn't matter if the function is declared before or after the call but the function should be there in the script and should be loaded in.
This is the first method and it will work:
some_func($a,$b);
function some_func($a,$b)
{
echo 'Called';
}
This is the second method and will also work:
function some_func($a,$b)
{
echo 'Called';
}
some_func($a,$b);
From the PHP manual:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
However, while this is more of a personal preference, I would highly recommend including all the functions you actually use in an external functions.php file then using a require_once() or include_once() (depending on tastes) at the very top of your main PHP file. This makes more logical sense -- if someone else is reading your code, it is blindingly obvious that you are using custom functions and they are located in functions.php. Saves a lot of guesswork IMO.
you can call a function before it's defined, the file is first parsed and then executed.
No.
It is not C :P...
As you can see here , the whole file is first being parsed and then executed.
If a function that doesn't exist is being called, php will throw an error.
Fatal error: Call to undefined function
As per my personal experience, In some special cases (Like, passing array's in function or function inside a function and so on). It's best option to define the function above the call. Because of this sometimes neither function works nor PHP throw an error.
In normal php functions, it doesn't matter. You can use both of the types.
It does not matter, as long as it is declared somewhere on the page.
as seen here:
http://codepad.org/aYbO7TYh
Quoting the User-defined functions section of the manual :
Functions need not be defined before
they are referenced, except when a
function is conditionally defined
So, basically : you can call a function before its definition is written -- but, of course, PHP must be able to see that definition, when try to call it.

PHP - override existing function [duplicate]

This question already has answers here:
Redefining PHP function?
(9 answers)
Closed 8 years ago.
Can I redeclare a existing function, with the same name, but different code? Or somehow "disable" the old function?
I want to redefince a core WordPress function, but since plugins and theme call this function a lot, I need to keep the same function name.
You could use the WordPress hooks (called filters and actions) and then use the add_filter() function to override the function you are using.
i.e.
function function_name() {
//code goes here
}
$hook = 'get_options'; // the function name you're filtering
add_filter( $hook, 'function_name' );
The Codex will help a lot with this.
http://codex.wordpress.org/Plugin_API/Filter_Reference
Only if you use something like APD that extends the zend engine to allow for that:
Intro
Override Method Docs
Note: Runkit seems like a better option than APD since it's more specific to this purpose and would allow you to keep the original method intact at a different address.
You can wrap the block defining the original function in a conditional checking if another of the same name is not already defined (I'm assuming you mean Wordpress functions and not core PHP ones)
<?php
if(!function_exists('function_name')){
//old definition here
} ?>
You could then redefine it above while still preserving the original should you need to roll back to it.
Depending on how complex the changes are and how many times you may do this, you may also want to look into Namespaces if you are on PHP 5.
Just comment the old function out and write your new one ;)
In PHP it is not possible to redefine or overload (i.e. define a function with the same name but different parameters) a function natively. There though are extensions like runkit which allow to redefine functions (runkit_function_redefine), but you probably don't want to use these (such extensions are rarely installed and mostly unreliable.)
My attempted solution was to do this:
function suffusion_get_image($options = array()) {
include_once ABSPATH.'wp-content/themes/suffusion-child/functions/media.php';
return childtheme_overide_suffusion_get_image($options = array());
....
}
Obviously there is an overhead at upgrade as you would need to add lines back into the scripts again and I have used this method successfully to date but now trying to do it with get_terms in the wp-includes and hitting a redeclaration issue which I am trying to resolve or workaround at the moment.
My reason to edit core is that the existing core does not provide in a convenient way for a multisite requirement.
Someone has just suggested on another forum however using override_function but the manual is worded such that it appears to be of use only to built-in functions - I took it that means PHP built in functions

Get a called functions list in PHP

In PHP, get_included_files() returns an array with the names of included files.
In a similar fashion, is there any way to get an array with the names of called functions with parameters?
In this way, Is any way to get an array with the names of called functions with parameters?
No.
What you can do is a debug_backtrace() which will show all the function calls (with parameters) that lead to the execution of the line you are doing the backtrace from (the "call stack"), but that's different from all functions that were ever called in the script.
What do you want to do? Maybe there's a different approach.
I was searching for something similar and found xdebug's tracing very useful.
Here's an example of how it could look like:
http://devzone.zend.com/1135/tracing-php-applications-with-xdebug/
I was trying to achieve what you want and finally came up with an reasonable solution.
Make a class named Debug and include that above every file you want to debug in. Build yourself a function that prints nicely the information stored in $calls.
class Debug {
private static $calls;
public static function log($message = null)
{
if(!is_array(self::$calls))
self::$calls = array();
$call = debug_backtrace(false);
$call = (isset($call[1]))?$call[1]:$call[0];
$call['message'] = $message;
array_push(self::$calls, $call);
}
}
Call this function everytime you declare a function first line in the functionbody: Debug::log($message(optional) )
Not that I'm aware.
You can however use debug_backtrace to get the currently active function/method hierarchy.
I don't think there's a way to do what you want. Sorry.
The closest I can get is the function_exists() function, which will tell you whether a specific function has been loaded.
What exactly do you want to achieve here? I can't see a use case (outside of a php_info() type screen) that would require a list of available functions.
You will have to install it as an extension, but a profiler like XHProf will give you a breakdown of which functions are called and how long they take, as well as a callgraph.
XHProf or Webgrind/KCachegrind will show you the functions called, but not their parameters.
You could also use get_defined_functions, which gives you a list of all functions defined. But it won't show you which functions have actually been called, and with what parameters.
If you really need to know the parameters, I don't know of any tools other than a custom logger like the one Henze provided in his answer.

Categories