Hướng dẫn php create function dynamically
Okay, challenge accepted!No matter how weird the question is (it's not btw), let's take it seriously for a moment! It could be useful to have a class that can declare functions and make them real:
That's it! Now they're real functions. No $-prefixing, no runtime evaluations whenever they get called - eval() was only needed once, for declaration. After that, they work like any function. Let's try them:
Magic behindHere's the class that can do this. Really not a complex one:
It provides an inner storage for your functions, and then declare "real" functions that call them. This is something similar to fardjad's solution, but with real code (not strings) and therefore a lot more convenient & readable. Callbacks can be denoted by the callable type declaration. Some functions like call_user_func() or usort() accept user-defined callback functions as a parameter. Callback functions can not only be simple functions, but also object methods, including static class methods. A PHP function is passed by its name as a string. Any built-in or user-defined function can be used,
except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset(). A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1. Accessing protected and private methods from within a class is allowed. Static class methods can
also be passed without instantiating an object of that class by either, passing the class name instead of an object at index 0, or passing Apart from common user-defined function, anonymous functions and arrow functions can also be passed to a callback parameter. Note: As of PHP 8.1.0, anonymous functions can also be created using the first class callable syntax. Generally, any object
implementing __invoke() can also be passed to a callback parameter. Example #1 Callback function examples // Type 6: Objects implementing __invoke can be used as callables Example #2 Callback example using a Closure The above example will output: Note: Callbacks registered with functions such as call_user_func() and call_user_func_array() will not be called if
there is an uncaught exception thrown in a previous callback. andrewbessa at gmail dot com ¶ 10 years ago
public function
public function
}
steve at mrclay dot org ¶ 9 years ago computrius at gmail dot com ¶ 8 years ago
walkIt($val) {
Riikka K ¶ 7 years ago
doStuff() { edanschwartz at gmail dot com ¶ 7 years ago
someAwesomeMethod() { InvisibleSmiley ¶ 1 year ago
bar(callable $c) {}$x = new X; Yzmir Ramirez ¶ 8 years ago
mariano dot REMOVE dot perez dot rodriguez at gmail dot com ¶ 6 years ago
bradyn at NOSPAM dot bradynpoulsen dot com ¶ 6 years ago
doSomethingFancy($arg1) gulaschsuppe2 at gmail dot com ¶ 3 years ago
function callbackFunc() { Daniel Klein ¶ 4 years ago
bar($value) { pawel dot tadeusz dot niedzielski at gmail dot com ¶ 6 years ago
someAwesomeMethod() { whysteepy at gmail dot com ¶ 5 years ago
chechomancr at hotmail dot com ¶ 7 months ago
Example { chris dot rutledge at gmail dot com ¶ 3 years ago
$isInstance = false; Dan J ¶ 4 years ago
|