从数组中调用方法和闭包
Calling methods and closures from an array
在JavaScript中,您可以执行以下操作:
var Module = (function () {
var functions = [method1, method2]; // array of functions to execute
function method1 () {
console.log('calling method1');
}
function method2 () {
console.log('calling method2');
}
function method3 () {
console.log('calling method3'); // not called
}
function add (fn) {
functions.push(fn); // add new function to the array
}
function printt () {
for (var i in functions) functions[i](); // execute functions in the array
}
return {
add: add,
printt: printt
};
})();
Module.add(function () {
console.log('calling anonymous function');
});
Module.printt();
// calling method1
// calling method2
// calling anonymous function
是否可以在PHP中执行类似的操作,将要执行的(1)方法存储在一个数组中,并将新的函数/方法添加到数组中,以便在运行printt
方法时执行数组中的所有函数?
class Module {
protected $functions = [];
public function __construct () {
// ?
}
protected function method1 () {
echo 'calling method1';
}
protected function method2 () {
echo 'calling method2';
}
protected function method3 () {
echo 'calling method3';
}
public function add ($fn) {
$this->functions[] = $fn;
}
public function printt () {
foreach ($this->functions as $fn) $fn();
}
}
$module = new Module();
$module->add(function () {
echo 'calling anonymous function';
});
$module->printt();
检查is_callable()的闭包和method_exists()的对象方法。
class Module {
protected $functions = ['method1', 'method2'];
// ...
public function printt () {
foreach ($this->functions as $fn) {
if ( is_callable( $fn ) ) {
$fn();
} elseif ( method_exists( $this, $fn ) ) {
$this->$fn();
}
}
}
}
与JS的区别还在于,您需要在对象中通过$this正确引用方法。
另一种方法是将成员方法作为可调用项而不仅仅是方法名添加到函数数组中,然后使用call_user_func执行它们。
class Module {
public function __construct() {
$this->functions = [
[$this, 'method1'],
[$this, 'method2'],
];
}
// ...
public function printt() {
foreach($this->functions as $fn) {
call_user_func($fn);
}
}
}
相关文章:
- 如何告诉闭包javascript编译器不要混淆webkitAudioContext的方法名称
- 这是在JavaScript中形成闭包的正确方法吗
- 如何测试只在闭包范围中可见的javascript方法
- JSHint 错误“对象不支持此属性或方法”用于 javascript 闭包
- 从数组中调用方法和闭包
- 这是一种捕获闭包定义堆栈的方法吗
- 如何强制 Google 闭包编译器重命名方法,即使使用对象也是如此
- 如何在ADVANCED_OPTIMIZATIONS的闭包编译器中导出公共类方法
- 将闭包编译器的类型安全性与AMD(requirejs)结合在一起的最可靠方法是什么?
- JavaScript 闭包和方法
- JQuery 通过闭包绑定到对象方法
- 有没有一种方法可以在谷歌闭包编译器中使用枚举作为类型
- 闭包得到了错误的值,如何找出解决方法
- javascript中的闭包与c++中的类实例(它有一个私有成员和一个公共方法)相当吗
- 闭包编译器未重命名属性和方法(高级编译)
- 如何在闭包之外调用方法
- 测试使用闭包中的变量的Javascript函数的最佳方法
- 使用闭包编译器时,导出库方法的最佳方式是什么
- JQuery中的闭包方法
- javascript中的闭包方法原型