문제

I wonder if this below is possible in php class object, just as I would do in javascript (jquery).

In jquery, I would do,

(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);

So, when I want to call a function inside myplugin, I just do this,

$.fn.myplugin("hello");

So, I thought, there might be a way to do this in php as well when you come to write a class?

$method = (object)array(
"init" => function() { 
   // I write the function here...   
}, 
"hello" => function() { 
 // I write the function here...    

} 
);

EDIT:

Could it be a class like this?

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...    

            } 
        );

    }

}
도움이 되었습니까?

해결책

Your $.fn.myplugin function is very similar to the __call() magic function in PHP. However, you would have to define it in a class and emulate the logic:

class Example {
    private $methods;

    public function __construct() {
        $methods = array();
        $methods['init'] = function() {};
        $methods['hello'] = function() {};
    }

    public function __call($name, $arguments) {
        if( isset( $methods[$name])) {
            call_user_func_array( $methods[$name], $arguments);
        } else if( $arguments[0] instanceof Closure) {
            // We were passed an anonymous function, I actually don't think this is possible, you'd have to pass it in as an argument
            call_user_func_array( $methods['init'], $arguments);
        } else {
            throw new Exception( "Method " . $name . " does not exist");
        }
    }
}

Then, you would do:

$obj = new Example();
$obj->hello();

It's not tested but hopefully it's a start.

다른 팁

PHP supports Closure (Anonymous functions) similar to jQuery take a look at

function x(callable $c){
     $c();
}

then use

x(function(){
     echo 'Hello World';
});
class ClassName {

    public function __construct(){
    //this is your init
    }

    public function hello(){
    //write your function here
    }

}

is how you would write it

then

$a =  new ClassName()

$a->hello();

to call it

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top