In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use the runkit extension in PHP. Many people may not know much about it. In order to make you understand better, the editor summarizes the following content for you. I hope you can get something according to this article.
Dynamically modify the constant define ('abnormal,' TestA')
Runkit_constant_redefine ('Aids,' NewTestA')
Echo A; / / NewTestA
Isn't it amazing. This runkit extension allows us to dynamically modify some constants, method bodies, and class extensions at run time. Of course, from a system security perspective, this extension is not highly recommended. Because the meaning of the constant itself is a constant, it should not be modified. Similarly, dynamically changing the content of a function body or class definition at run time may affect other code that calls these functions or classes, so this extension is a dangerous extension.
In addition to dynamically modifying constants, we can also use the runkit_constant_add () and runkit_constant_remove () functions to dynamically add or remove constants.
Installation
The installation of the runkit extension needs to be downloaded from githup and then compiled normally. Pecl downloads are out of date.
PHP5: http://github.com/zenovich/runkit
PHP7: https://github.com/runkit7/runkit7.git
After the clone is successful, you can go through the normal extension compilation and installation steps.
Phpize
. / configure
Make
Make install
Different versions of PHP need to install different versions of the extension. At the same time, runkit7 is still under development, and some functions are not supported, such as:
Runkit_class_adoptrunkit_class_emancipaterunkit_importrunkit_lint_filerunkit_lintrunkit_sandbox_output_handlerrunkit_return_value_usedRunkit_SandboxRunkit_Sandbox_Parent
None of the above functions or classes are supported when writing the test code for this article. You can use the PHP5 environment to test whether the original extensions can be used properly.
View the hyperglobal variable key print_r (runkit_superglobals ())
/ / Array
/ / (
/ / [0] = > GLOBALS
/ / [1] = > _ GET
/ / [2] = > _ POST
/ / [3] = > _ COOKIE
/ / [4] = > _ SERVER
/ / [5] = > _ ENV
/ / [6] = > _ REQUEST
/ / [7] = > _ FILES
/ / [8] = > _ SESSION
/ /)
This function is actually looking at all the hyperglobal variable key names in the current runtime environment. These are some of our commonly used hyperglobal variables, so we will not explain them one by one.
Method-related operation
Method operations are the same as constant operations, we can dynamically add, modify, delete, and rename various methods. First of all, let's take a look at the logical code that we are most concerned about modifying the method body at dynamic runtime.
Function testme () {
Echo "Original Testme Implementation\ n"
}
Testme (); / / Original Testme Implementation
Runkit_function_redefine ('testme','','echo "New Testme Implementation\ n";')
Testme (); / / New Testme Implementation
A testme () method is defined, and then its implementation is modified through runkit_function_redefine (). Finally, when testme () is called again, the output is the newly modified implementation. So, can we modify the methods that come with PHP?
/ / php.ini runkit.internal_override=1
Runkit_function_redefine ('str_replace',', 'echo "str_replace changed!\ n";')
Str_replace (); / / str_replace changed!
Runkit_function_rename ('implode',' joinArr')
Var_dump (joinArr (",", ['await,' baked,'c']))
/ / string (5) "a dint bjorn c"
Array_map (function ($v) {
Echo $vJet PHProomEOL
}, [1pm 2pm 3])
/ / 1
/ / 2
/ / 3
Runkit_function_remove ('array_map')
/ / array_map (function ($v) {
/ / echo $v
/ /}, [1, 2, 3])
/ / PHP Fatal error: Uncaught Error: Call to undefined function array_map ()
The comments in the code make it clear that we only need to set runkit.internal_override=1 in php.ini to dynamically modify the method functions that come with PHP. For example, in the first paragraph, we modified the str_replace () method to let him output a paragraph of text directly. Then we rename implode () to joinArr (), and we can use this joinArr () just like implode (). Finally, we removed the array_map () method, and if we call this method again, we will get an error.
Operations related to class methods
The operation of the method function inside the class is similar to the ordinary method operation above, but we cannot modify, delete and so on the class that comes with PHP. You can try this for yourself.
/ / runkit_method_add ('PDO',' testAddPdo',', 'echo "This is PDO new Func!\ n";')
/ / PDO::testAddPdo ()
/ / PHP Warning: runkit_method_add (): class PDO is not a user-defined class
From the error message, you can see that the PDO class is not a user-defined class, so you cannot use the runkit function for related operations. Let's take a look at how our custom classes use runkit for dynamic operations.
Class Example {
}
Runkit_method_add ('Example',' func1',', 'echo "This is Func1!\ n";')
Runkit_method_add ('Example',' func2', function () {
Echo "This is Func2!\ n"
});
$e = new Example
$e-> func1 (); / / This is Func1!
$e-> func2 (); / / This is Func2!
Runkit_method_redefine ('Example',' func1', function () {
Echo "New Func1!\ n"
});
$e-> func1 (); / / New Func1!
Runkit_method_rename ('Example',' func2', 'func22')
$e-> func22 (); / / This is Func2!
Runkit_method_remove ('Example',' func1')
/ / $e-> func1 ()
/ / PHP Fatal error: Uncaught Error: Call to undefined method Example::func1 ()
We defined an empty class, then dynamically added two methods to it, then modified method 1, renamed method 2, and finally deleted method 1. A series of operations are basically the same as the common methods above.
Summary
As mentioned above, this extension is a dangerous one, especially if we turn on runkit.internal_override, we can also modify the native functions of PHP. However, if it is necessary to use it, then its features are very useful. Just like the Visitor pattern, "most of the time you don't need the Visitor pattern, but once you need the Visitor pattern, you really need it." the same is true of this set of runkit extensions.
After reading the above, do you have any further understanding of how to use runkit extensions in PHP? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.