1

I am trying to call a method from a different class in a different file. However, When I run the code i get the following error:

Fatal error: Call to undefined function getFxRate() in fxCalc.php

Here is the code I am trying to build:

fxCalc.php

//call fxDataModel class
require_once('fxDataModel.php');
$fxRate = getFxRate($inputCurrency, $outputCurrency);
$txtOutput = $txtInput * $fxRate;

fxDataModel.php

   public static function getFxRate($inputCurrency, $outputCurrency)
    {
        $fxRate = $currencies[$inputCurrency][$outputCurrency];
        return $fxRate;
    }

Any help would be appreciated.

3
  • 3
    The function getFxRate() is a static method of another class. You need to call it as OtherClassName::getFxRate($inputCurrency, $outputCurrency) Commented Feb 23, 2014 at 2:19
  • @MichaelBerkowski I think that would be a better answer than a comment Commented Feb 23, 2014 at 2:20
  • The docs on how static methods are scoped and called... Commented Feb 23, 2014 at 2:21

1 Answer 1

2

Say if your name of the class infxDataModel.php is FxModel

Then you need to call your method like

$fxRate = FxModel::getFxRate($inputCurrency, $outputCurrency);

instead of

$fxRate = getFxRate($inputCurrency, $outputCurrency);

That is because your function is declared as static. The static calls are made by using :: operator followed by the class name.

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.