简体   繁体   中英

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

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.

You need to write a function that will take your query, log the sql first, runs your query, then return the results.

EG

<?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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM