我不知道下面這個是否可能出現在php類對象中,就像我在javascript(jquery)中那樣。PHP類:寫對象內的函數?
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...
}
);
}
}
謝謝,但不應該是'$ obj = new例子();'和'public function Example(){}'? – laukok
@lauthiamkok - 因此「沒有測試」......我會做出一些小的改變。 – nickb