Php 如何在对象上下文中正确使用$this?

Php 如何在对象上下文中正确使用$this?,php,netbeans,web,Php,Netbeans,Web,我很难使用$this在php类中创建get函数: public function getFxRate($currencyInputCode, $currencyOutputCode) { //Getting the currency return $this->fxRates[$currencyOutputCode][array_search(1.0, $this->fxRates[$currencyInputCode])]; } $fxRates数组在此处分配:

我很难使用$this在php类中创建get函数:

public function getFxRate($currencyInputCode, $currencyOutputCode) {
    //Getting the currency
    return $this->fxRates[$currencyOutputCode][array_search(1.0, $this->fxRates[$currencyInputCode])];
}
$fxRates数组在此处分配:

public function __construct() {
    $this->ini = parse_ini_file(FXCALC);
    $handle = fopen($this->getIniArray()[self::RATESFILE], 'r');
    $this->fxCurriences = fgetcsv($handle);
    $k = 0;
    for ($i = 0; !feof($handle); $i++) {
        $tempMultiArray[$i] = fgetcsv($handle);
    }
    fclose($handle);

    foreach ($this->getCurrencies() as $value) {
        for ($i = 0; $i < count($this->getCurrencies()); $i++) {
        $this->fxRates[$value][$i] = $tempMultiArray[$i][$k];
        }
        $k++;
    }
    echo $this->fxRates['CAD'][0];
}
我不明白为什么$this会显示上面get函数的错误,但这两个函数工作正常:

public function getIniArray() {
    return $this->ini;
}

public function getCurrencies() {
    return $this->fxCurriences;
}
以下是我收到的错误:


致命错误:在第65行的C:\xampp\htdocs\PhpMidtermBP\FxDataModel.php中不在对象上下文中时使用$this

$这指的是一个实例,它来自您看到的错误,您正在静态调用该方法

您需要实例化该类,然后运行getFxRate方法使$this工作

例如:


这个错误消息到底是什么?考虑到可能出现的错误数量几乎是无限的,除了“修复错误”之外,我们无法为您提供任何帮助。@MarcB Ok我在上面添加了错误。叹气,这是第65行?@Dagon第65行是return$this->return$this->fxRates[$currencyOutputCode][array_search(1.0,$this->fxRates[$currencyInputCode]);在getFxRate函数下。错误消息表示您正在静态调用该函数,例如,
YourObj::getFxRate(…)
。在静态模式下,
$this
不可用。
public function getIniArray() {
    return $this->ini;
}

public function getCurrencies() {
    return $this->fxCurriences;
}
$fx = new MyFxClass();
$fx->getFxRate( $arg1, $arg2 );