简体   繁体   English

来自2个不同库的php中的函数名冲突

[英]Function name conflict in php from 2 different libraries

I have 2 'libraries' which I need to include on the same page. 我有2个'库',我需要包含在同一页面上。 Simple Machine Forums and Wordpress. 简单的机器论坛和Wordpress。

However both have the function is_admin() which conflicts with each other. 但是两者都具有相互冲突的函数is_admin()。

Fatal error: Cannot redeclare is_admin() (previously declared
in /home/site.com/wordpress/wp-includes/query.php:100)in /home/site.com/smf/Sources/Security.php on line 82)

What would be the best way to get around this? 什么是最好的解决方法? As I dont want to have to modify all calls to one library to be is_admin2() for example. 因为我不想将对一个库的所有调用修改为is_admin2()。

I believe you don't have too much choice but to rename the function or, wrap all functions around a class. 我相信你没有太多的选择,只能重命名函数或包装一个类的所有函数。 That's the problem with PHP <= 5.*: no namespaces, and developers often prefer to write a script full of loose functions, than to use an object oriented approach. 这就是PHP <= 5. *的问题:没有名称空间,开发人员通常更喜欢编写一个充满松散函数的脚本,而不是使用面向对象的方法。

I would bite the bullet manually rename each function call for the smaller library (I'm guessing that would be the Forums one). 我会咬住子弹手动重命名为较小的库的每个函数调用(我猜这将是论坛之一)。 Good luck. 祝好运。

Rename the functions to is_admin_wp() and is_admin_smf() . 将函数重命名为is_admin_wp()is_admin_smf() Then, define your own is_admin() function. 然后,定义自己的is_admin()函数。 This could be just a simple wrapper: 这可能只是一个简单的包装器:

function is_admin() {
    // from what function is_admin was called?
    list (, $last) = debug_backtrace();
    if (strpos($last['file'], 'wordpress') >= 0) {
        $fn = 'is_admin_wp';
    } else {
        $fn = 'is_admin_smf';
    }
    $args = func_get_args();
    return call_user_func_array($fn, $args);
}

A possibility would be to use PHP 5.3, which brings support for namespaces. 可能的方法是使用PHP 5.3,它支持命名空间。 I don't know the details of the implementation but it should enable you to wrap the libraries in different namespaces. 我不知道实现的细节,但它应该使您能够将库包装在不同的命名空间中。

Unfortunately 5.3 has still not hit the stable release. 不幸的是5.3仍未达到稳定版本。 If you can't/don't want to use it, the only option I see that doesn't involve renaming would be this: create separate two PHP scripts, start them in different interpreters, and have them communicate somehow (a pipe, socket, tempfile). 如果您不能/不想使用它,我看到的唯一不涉及重命名的选项是:创建单独的两个PHP脚本,在不同的解释器中启动它们,并让它们以某种方式进行通信(管道, socket,tempfile)。

Luckly for me this was the comment found in the decleration of is_admin in Simple Machine Forums. 幸运的是,这是简单机器论坛中is_admin的删除中发现的注释。

// Grudge chickens out and puts this in for combatibility. This will be ripped out on day one for SMF 1.2 though ;)

Seems its not needed anyway... A little anoying that I will have to remember to remove this when I upgrade every time though... 似乎它不需要它...有点烦人,我必须记住在我每次升级时删除这个...

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

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