Translate class function to lambda function
I'm trying to pull of something like this:
cl开发者_如何学运维ass helper {
.. some class variables ..
public function somehelper ($somevar)
{
.. some action ..
}
}
to - $somehelper('somevar!')
i want it to be a lambda function without me declaring a new lambda function. I want the code to be created automatically. I want to create this lambda function autamatically via a core class that will create to each helper function - lambda function with the functions name. I dont want to specify it. I have a template file included, and an instance of a class helper. <a href..><?= $makeSeo($url); ?></a>
this will lead to - public function makeSeo($url)
in a helper class. autmatically.
Thanks in advanced!
Sounds like you want something to create a lambda given a method name? Assuming your 'Helper' class methods are static, something like this would do it
function makeHelper($fn)
{
//note use of PHP5.3 closure syntax
$helper = function($param) use ($fn) {
return Helper::$fn($param);
};
return $helper;
}
//create a new lambda
$foo=makeHelper('somehelper');
//call it...
$foo('Hello world);
I think you're saying that you want the variable $somehelper
to call helper::somehelper
from outside the context of the class.
$somehelper = function($somevar) {
return helper::somehelper($somevar);
});
This presume that the somehelper
is intended to be a static function (i.e. should be marked as public static function
. If it is not, you're going to have to provide more information about where the class instance comes from.
精彩评论