1

This is how I am reaching the _call method:

$model->delivery_price = $currencyConverter->convertPriceByGivenCurrencies(
                        $model->delivery_price,
                        $currency->id,
                        $model->order_currency
                    );

The function throws an error but the method exists below it. My __call looks like:

public function __call($name, $params)
    {
        if(method_exists(CurrencyConverter::className(), $name)){
            if($params[0] == 0 || $params[0]){
                call_user_func_array($name, $params);
            }else{
                throw new \Exception('Price must be a valid number!');
            }
        }
        throw new NotFoundException('Function doesn\'t exist');
    }

It passes the if condition but after that the error occures:

call_user_func_array() expects parameter 1 to be a valid callback, function 'convertPriceByGivenCurrencies' not found or invalid function name

And this is the convertPriceByGivenCurrencies method which is landed in the below the _call:

protected function convertPriceByGivenCurrencies($product_price, $product_price_currency_id, $select_currency_id)
    {
      ............
    }

What am I doing wrong here ? Thank you!

1
  • When you are checking whether the method name exists, you are passing a class name to do so. But where in your call_user_func_array call are you referring to that same class …? You are passing the function name only there, so it looks for a standalone function of that name. Commented Nov 13, 2018 at 7:44

2 Answers 2

1

$name by itself is not a known function; it seems to be a method in the CurrencyConverter class.

So to call it, assuming it is a static method, you would need something like:

CurrencyConverter::$name(...$params);

Note that you need the ... operator to unpack $params

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

5 Comments

But the unpacking is avaible in 5.6 only. At least this is what the PHPStorm says :)
@TomaTomov That's right, if you are on an earlier version you should probably upgrade :-)
In fact I am on PHP Version 7.2.6-1. it is not usable on higher version ?
@TomaTomov It is available from 5.6 on, so you can use it without any problems. Does PHPStorm know you are on 7.2?
Guess he doesn't know because it is working :) Thank you very much!
1

Calling it with

call_user_func_array($name, $params);

it is expecting a standalone function called $name.

As it's a method in a class you need to add this information to the callable, if you want to call it on the current instance then use

call_user_func_array(array($this,$name), $params);

If it isn't a method in the current instance, then replace $this with the appropriate instance. Or change the method to be static and replace $this with the class name.

2 Comments

__call and protected function convertPriceByGivenCurrencies are both in CurrencyConverter class. And the variable $currencyConverter is instance of the CurrencyConverter class. Sorry about the unclear explanation :)
Your answer also works and both gave me some new knowledges. I wish I could mark more than 1 best answers. Thank you very much!

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.