开发者

PHP runkit_function_rename don't work?

开发者 https://www.devze.com 2023-02-28 23:02 出处:网络
This code don\'t work. Why not? <?php function test() { echo \'test\'; } runkit_function_rename(\'test\', \'test2\');

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. When I'm on localhost I want that function to do something different. I want to override the function with own stuff.

All alternatives are welcome as well.


Do you have the PECL extension installed?

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.

You asked for alternatives, and I can definitely recommend this one:

Patchwork

Patchwork is a PHP function-override library. In other words, it does much the same job as Runkit.

The main difference is that it is written in pure PHP - no extensions to install; just a require_once() at the top of your code.

The flip side of this is that because it's pure PHP, it can only replace functions defined within your program; ie it can't override a PHP built-in function like Runkit can. The example in your question will work fine with Patchwork, but trying to override a PHP function like mysql_query() is not possible.

However, unlike Runkit, it works perfectly, so if you can live with that limitation, I'd strongly recommend it.

Another alternative to Runkit that you might want to try is PHP Test Helpers. This is a PHP extension, and covers pretty much the same ground as Runkit. It's written by the same author as PHPUnit, so it should be pretty good. 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). Neither Runkit nor PHP Test Helpers are provided with Windows executables; in order to use either of them in Windows you need to compile the extension yourself from the C source code. For this reason, if you're on Windows, then Patchwork is your only sensible choice.


What I really want is this. I'm using a system that have a function. When I'm on localhost I want that function to do something different. I want to override the function with own stuff.

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.


As explained earlier, it's probably best to differentiate inside of a function body regarding the value of $_SERVER['HTTP_HOST'].

Although I'd personally see this as bad style, you can even define function inside of other functions or blocks.

This snippet defines one 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.


Someone might also experience that runkit_function_* functions are not working although the runkit library is installed correctly. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消