0

I have a variable that holds a class name.

public $modelClass = 'common\models\Notecard';

That class has a static method.

public static function do_something() { ... }

Given this information, I would like to call the static function. For non-static functions, I can do the following:

$model_name = $this->modelClass;
$model = new $model_name();
$model->do_something_else();

2 Answers 2

2

Yes, that is pretty easy: You can either just call the function from your instance e.g.

$model_name = $this->modelClass;
$model = new $model_name();
$model::do_something();

or using call_user_func()

call_user_func([$modelClass, 'do_something']);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use $model_name::do_something_else().

class foo {
    public static function bar() {
        echo "Called bar";
    }
}

$fname = "foo";
$fname::bar();

Outputs Called bar

Works on php7.

1 Comment

I appreciate the answer, but I am using php5.5 and this won't work. I had indicated this in the tags but not in my question itself. Will edit my question to clear this up. Thanks :)

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.