我想知道这在 php 类对象中是否可能,就像我在 javascript (jquery) 中所做的那样。
在 jquery 中,我会这样做,
(function($){
var methods = {
init : function( options ) {
// I write the function here...
},
hello : function( options ) {
// I write the function here...
}
}
$.fn.myplugin = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist.' );
}
return this;
};
})(jQuery);
所以,当我想在里面调用一个函数时myplugin
,我就这样做,
$.fn.myplugin("hello");
所以,我想,当你来写一个类时,可能有一种方法可以在 php 中做到这一点?
$method = (object)array(
"init" => function() {
// I write the function here...
},
"hello" => function() {
// I write the function here...
}
);
编辑:
会是这样的课吗?
class ClassName {
public function __construct(){
//
}
public function method_1(){
$method = (object)array(
"init" => function() {
// I write the function here...
},
"hello" => function() {
// I write the function here...
}
);
}
public function method_2(){
$method = (object)array(
"init" => function() {
// I write the function here...
},
"hello" => function() {
// I write the function here...
}
);
}
}