简体   繁体   English

PHP runkit_function_rename 不起作用?

[英]PHP runkit_function_rename don't work?

This code don't work.此代码不起作用。 Why not?为什么不?

<?php
function test()
{
    echo 'test';
}
runkit_function_rename('test', 'test2');
test2();
?>

What I really want is this.我真正想要的是这个。 I'm using a system that have a function.我正在使用具有 function 的系统。 When I'm on localhost I want that function to do something different.当我在本地主机上时,我希望 function 做一些不同的事情。 I want to override the function with own stuff.我想用自己的东西覆盖 function。

All alternatives are welcome as well.也欢迎所有替代方案。

Do you have the PECL extension installed?您是否安装了 PECL 扩展?

http://www.php.net/manual/en/runkit.installation.php http://www.php.net/manual/en/runkit.installation.php

This » PECL extension is not bundled with PHP.

I never had any luck with Runkit either.我也从来没有遇到过 Runkit。

You asked for alternatives, and I can definitely recommend this one:你要求替代品,我绝对可以推荐这个:

Patchwork拼凑而成

Patchwork is a PHP function-override library. Patchwork 是一个 PHP 函数覆盖库。 In other words, it does much the same job as Runkit.换句话说,它的工作与 Runkit 大致相同。

The main difference is that it is written in pure PHP - no extensions to install;主要区别在于它是用纯 PHP 编写的 - 无需安装扩展; just a require_once() at the top of your code.只是代码顶部的require_once()

The flip side of this is that because it's pure PHP, it can only replace functions defined within your program;不利的一面是,因为它是纯 PHP,它只能替换程序中定义的函数; ie it can't override a PHP built-in function like Runkit can.即它不能像 Runkit 那样覆盖 PHP 内置 function 可以。 The example in your question will work fine with Patchwork, but trying to override a PHP function like mysql_query() is not possible.您问题中的示例适用于 Patchwork,但无法尝试像mysql_query()那样覆盖 PHP function 。

However, unlike Runkit, it works perfectly, so if you can live with that limitation, I'd strongly recommend it.然而,与 Runkit 不同的是,它运行良好,所以如果你能忍受这个限制,我强烈推荐它。

Another alternative to Runkit that you might want to try is PHP Test Helpers .您可能想尝试的 Runkit 的另一种替代方法是PHP Test Helpers This is a PHP extension, and covers pretty much the same ground as Runkit.这是一个 PHP 扩展,涵盖与 Runkit 几乎相同的领域。 It's written by the same author as PHPUnit, so it should be pretty good.和PHPUnit是同一作者写的,应该还不错。 However I didn't have much joy when I tried to install this either, so I can't really comment on it much.但是,当我尝试安装它时,我也没有太多的乐趣,所以我不能对此发表太多评论。

I note from your comments elsewhere on this question that you're running Windows (ie WAMP).我从您在其他地方对此问题的评论中注意到,您正在运行 Windows(即 WAMP)。 Neither Runkit nor PHP Test Helpers are provided with Windows executables; Runkit 和 PHP 测试助手都没有提供 Windows 可执行文件; in order to use either of them in Windows you need to compile the extension yourself from the C source code.为了在 Windows 中使用它们中的任何一个,您需要自己从 C 源代码编译扩展。 For this reason, if you're on Windows, then Patchwork is your only sensible choice.因此,如果您使用的是 Windows,那么 Patchwork 是您唯一明智的选择。

Someone might also experience that runkit_function_* functions are not working although the runkit library is installed correctly.尽管正确安装了 runkit 库,但有些人可能还会遇到 runkit_function_* 函数不起作用。 This is because these functions are broken for some PHP versions (probably at least all 5.2.*) as can be seen here: https://bugs.php.net/bug.php?id=58205这是因为这些功能在某些 PHP 版本(可能至少所有 5.2.*)中被破坏,如下所示: https://bugs.php.net/bug.ZE1BFD762321E40963CEE4ZACid=528C

What I really want is this.我真正想要的是这个。 I'm using a system that have a function.我正在使用具有 function 的系统。 When I'm on localhost I want that function to do something different.当我在本地主机上时,我希望 function 做一些不同的事情。 I want to override the function with own stuff.我想用自己的东西覆盖 function。

All alternatives are welcome as well.也欢迎所有替代方案。

function test() {
  if($_SERVER['HTTP_HOST'] == 'localhost' {
     // do one thing
  } else {
     // do other thing
  }
}

If you're set on using runkit, you'd need to use runkit_function_redefine , not runkit_function_rename to make the same function do different things.如果您打算使用 runkit,则需要使用runkit_function_redefine而不是runkit_function_rename来使相同的 function 做不同的事情。

As explained earlier, it's probably best to differentiate inside of a function body regarding the value of $_SERVER['HTTP_HOST'].如前所述,最好在 function 主体内部区分 $_SERVER['HTTP_HOST'] 的值。

Although I'd personally see this as bad style, you can even define function inside of other functions or blocks.虽然我个人认为这是不好的风格,但您甚至可以在其他函数或块中定义 function。

This snippet defines one function get_template_part():这个片段定义了一个function get_template_part():

if($_SERVER['HTTP_HOST'] == 'localhost' {

  function get_template_part() {
  }

} else {

  function get_template_part() {
  }

}

Unfortunately, this wouldn't help in your case, since get_template_part() is already defined outside your reach.不幸的是,这对您的情况没有帮助,因为 get_template_part() 已经在您的范围之外定义。

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

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