1

I'd like to create a variable that 'points' to a class method like this:

$obj = $this->myMethod();
$obj->call();

or

$obj = $myClass->myMethod();
$obj->call();

Is this possible?

Edit:

Why I'd like to be able to do this:

private $default_field_callback = array(
        "methods" => array(
            "timestamp" => array(
                "object" => {object},
                "method" => "getTimestamp",
                "params" => array("Y-m-d H:i:s")
            ),
        )
    );
4
  • 1
    $obj = $this->myMethod() will result in $obj containing whatever $this->myMethod(); returns, not an "instance" of $this->myMethod(). Why do you want to do this? Commented Jun 6, 2014 at 1:44
  • I realize that, but I don't want to call $this->myMethod(), I just want a callable object. Commented Jun 6, 2014 at 1:44
  • 1
    What's the point of doing this? Can you give us more detail please? Commented Jun 6, 2014 at 1:44
  • I'd like to have an array of callback methods for validating a form. Commented Jun 6, 2014 at 1:45

1 Answer 1

3

you can make use of call_user_func() with this array structure indicating a class/method call.

$result = call_user_func(array($this,'myMethod'));

http://www.php.net/function.call-user-func read up for more information and parameter usage

Edit: see also call_user_func_array() to feed in your parameters as a single array

http://www.php.net/manual/en/function.call-user-func-array.php

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

Comments

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.