PHP runkit_function_rename not working?

This code does not work. Why not?

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

I really want this. I use a system that has a function. When I'm on the local host, I want this function to do something else. I want to redefine the function with my own things.

All alternatives are also welcome.

+4
source share
5 answers

Do you have the PECL extension installed?

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

 This Β» PECL extension is not bundled with PHP. 
+7
source

I was not even lucky with Runkit.

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

Patchwork

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

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

The reflective side of this is that since it is pure PHP, it can only replace the functions defined in your program; those. it cannot override a PHP built-in function such as Runkit. The example in your question will work just fine with Patchwork, but trying to override a PHP function like mysql_query() is not possible.

However, unlike Runkit, it works just fine, so if you can live with this limitation, I highly recommend it.

Another Runkit alternative you can try is the PHP Help Helpers . This is an extension of PHP and covers almost the same foundation as Runkit. It is written by the same author as PHPUnit, so this should be very good. However, I did not have much joy when I tried to fix this, so I cannot comment on it.

I remember your comments elsewhere on this subject that you are using Windows (i.e. WAMP). Neither Runkit nor PHP Test Helpers provide Windows executables; To use any of them on Windows, you need to compile the extension yourself from the C source code. For this reason, if you are working on Windows, then Patchwork is your only reasonable choice.

+3
source

I really want this. I use a system that has a function. When I'm on the local host, I want this function to do something else. I want to redefine the function with my own things.

All alternatives are also welcome.

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

If you are configured to use runkit, you need to use runkit_function_redefine , not runkit_function_rename , to make the same function in different ways.

+1
source

As explained earlier, it is best to distinguish the value of $ _SERVER ['HTTP_HOST'] inside the function body.

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

This snippet defines one get_template_part () function:

 if($_SERVER['HTTP_HOST'] == 'localhost' { function get_template_part() { } } else { function get_template_part() { } } 

Unfortunately, this will not help in your case, since get_template_part () is already defined out of your reach.

+1
source

It may also happen to someone that the runkit_function_ * functions do not work, although the runkit library is installed correctly. This is due to the fact that these functions are violated for some versions of PHP (perhaps at least 5.2. *), As you can see here: https://bugs.php.net/bug.php?id=58205

+1
source

Source: https://habr.com/ru/post/1275607/


All Articles