php,我在使用$ this->时在课堂上出错

zor*_*404 1 php

我使用时在代码中出现此错误 $this->

致命错误:无法访问第21行/home/u506382626/public_html/s1/arrays.php中的空属性

我虽然这是你如何访问当前的对象变量,不是吗?

这是我的代码.我正在尝试为每个索引创建一个具有更大值的数组但是使用函数at而不是索引.是否有可能在php中重载operator [],你是如何做到的?

希望有人知道这是什么问题.

<?php

class zArray {
    public $change = 1.165;

    public $lenght;
    private $vars = array();
    public $name;
    public $percent = false;

    public function __construct($values) {
        for ($i=0; $i < 5; $i++) { 
            $vars[$i] = $values[$i];
        }
        $lenght = $values[5];
        $name = $values[6];
        $percent = $values[7];
    }

    public function at($i) {
        $newchange = $this->$change;
        for ( $i-- ; $i; $i-- )
            $newChange *= $this->$change;

        $newVars = $this->$vars;
        for ($i = 0; $i < 4; $i++) { 
            $newVars[$i] *= $newChange;
        }
        if ($percent) $newVars[4] /= $newChange;
        else $newVars[4] *= $newChange;

        return $newVars;
    }
};

$buildings = array(
    new zArray(85, 70, 65, 2, 100, 'Castle', true)
);

$try = $buildings[0]->at(3);



?>
Run Code Online (Sandbox Code Playgroud)

Kha*_*han 7

您无法像这样访问变量

$this->$change; //wrong
Run Code Online (Sandbox Code Playgroud)

试试这个吧

$this->change;
Run Code Online (Sandbox Code Playgroud)