Can I extend the php function?

My question is whether the declared function can be extended.

I want to extend mysql_function to add a mysql query that inserts several logs into the table: 'query' - mysql_query parameter, date, page ... etc

+4
source share
6 answers

My question is whether the declared function can be extended.

No.

You can extend the class method and call parent::methodname() to run the previous code (which is almost what you are asking for), but there is no way for ordinary functions to do this.

There are several esoteric PHP extensions that allow you to redefine functions, but I believe that not what you need, but their use is rarely practical.

What you probably want to do is create a new function and call an existing function in it.

+6
source

No, you cannot do this. Either enable MySql Query Logs , or wrap the query code in Decorator Logging, or use an abstraction like Zend_Db, which can accept Profiler or use the transparent mysqlnd plugin

+2
source

You need to write a function that will take your query, write sql first, execute your query and 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 does not extend the function, but you can extend the class

0
source

It's impossible.

You must create your own API (or use an existing one) to access the database, so when you need to log, you can simply improve your own API function. It is also very convenient if you need some error handling function. Update the code.

0
source

from http://php.net/manual/en/function.rename-function.php

bool rename_function (line $ original_name, line $ new_name)

Renames the name 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, add a replace function that executes your log, and then call original_mysql_query, etc., that you will achieve your goal, assuming you have a way to enter a rename on each page, it is called MySQL_query. Most large sites have common code that is included at the top of every page that can do this for you.

There is also a built-in php function called override_function (mentioned by ChrisH). It is not fully documented on the php man page, but the user comments below the document give you the information you need to use it if you prefer the rename_function function. The limitation of a single override was discussed if you needed to call the original function from the substitution. Using rename_function instead of overrides eliminates this potential limitation.

0
source

All Articles