1

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

            } 
        );

    }

}

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but shouldn't they are $obj = new Example(); and public function Example() {}??
@lauthiamkok - Hence "It's not tested"... I'll make the small changes.
1

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

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

then use

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

Comments

0
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

2 Comments

you want an anonymous class ?
Maybe but what is an anonymous class? never did it before. What good about an anonymous class??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.