简体   繁体   English

有可能扩展php功能吗?

[英]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 我想扩展mysql_function来添加mysql查询,将一些日志插入表中:'query' - mysql_query,date,page ...等参数

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. 你可以扩展一个类方法并调用parent::methodname()来运行前面的代码(这几乎就是你要求的),但是对于普通函数,没有办法做到这一点。

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. 有一些深奥的PHP扩展允许覆盖功能,但我认为这不是你需要的,它们的使用很少实用。

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 启用MySql查询日志或将执行查询的代码包装到Logging Decorator中,或使用Zend_Db之类的抽象,可以使用Profiler或使用透明日志插件来实现mysqlnd

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

bool rename_function ( string $original_name , string $new_name ) bool rename_function(string $ original_name,string $ new_name)

Renames a orig_name to new_name in the global function table. 在全局函数表中将orig_name重命名为new_name。 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. 我相信如果你将原始文件重命名为original_mysql_query,那么添加你的替换函数来执行你的日志记录,然后调用original_mysql_query等,你将实现你的目标,假设你有办法在每个调用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). 还有一个内置的php函数叫做override_function(由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. 它没有在php手册页中完整记录,但是如果您更喜欢rename_function函数,那么doc下面的用户注释会为您提供使用它所需的信息。 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. 使用rename_function而不是override函数可以消除这种潜在的限制。

You need to write a function that will take your query, log the sql first, runs your query, then return the results. 您需要编写一个将接受查询的函数,首先记录sql,运行查询,然后返回结果。

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. 您应该已经创建了自己的API(或使用现有的API)来访问数据库,因此当您需要日志记录时,您可以简单地增强自己的API函数。 It also comes very handy if you need some custom error handling function. 如果您需要一些自定义错误处理功能,它也非常方便。 Refactor the code. 重构代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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