V
V
Viktor Yanyshev2018-05-21 00:13:52
Yii
Viktor Yanyshev, 2018-05-21 00:13:52

Why is the method and class not found not called?

When calling a static method, it throws an error: "call_user_func() expects parameter 1 to be a valid callback, class 'BA::class' not found". Why doesn't call_user_func work and why is 'BA::class' not found? If the namespace of the model where the getProduct() method is the same common\models as the model (any models that are twitched by the getProduct method will and have the same namespace) is 'BA::class' .

public function getProduct() {
        $category = Category::findOne($this->category_id);

        if(!is_null($category->parent_id)) {
            $category = Category::findOne($category->parent_id);
        }

        $model = $category->title;

        if(count($model = explode(' ', $model)) > 1) {
            for($i = 0; count($model) > $i; $i++) {
                $model[$i] = ucfirst($model[$i]);
            }
            $model = implode($model, '');
        } else {
            $model = ucfirst($model[0]);
        }

        $model = $model.'::class';
        $model = call_user_func(model);

        return $this->hasOne($model, ['id' => 'product_id']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gerasimov, 2018-05-21
@mrTeo

Calling a static class method is done by passing an array as the first argument
call_user_func(array('MyClass', 'myCallbackMethod'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question