-1

我正在尝试通过将其从类传递给另一个函数来修改已创建的对象。在我下面的场景中,我希望结果为 2,但我得到一个“未捕获的错误”。

------------------------------
Index.php
------------------------------
include "class.php";
include "function.php";

$number = new a();

$number->add(1);
echo $number->display();

$number->add_helper();
echo $number->display();

------------------------------
function.php
------------------------------
function get_help(&$numb) {
    $numb->add(1);
}

------------------------------
class.php
------------------------------

class a {

    protected $var;

    public function add($numb) {
        $this->var .= $numb;
    }

    public function display() {
        return $this->var;
    }

    public function add_helper() {
        get_help($this->var);
    }
}

------------------------------

当我运行 add_helper 函数时,就会发生错误。我期望结果为 2。

在此先感谢您的帮助。

将要

4

1 回答 1

0

感谢 Magnus Eriksson 的回复……

当我需要传递对象时,我正在传递字符串。

我的类中的 add_helper 函数实际上应该是:

    get_help($this);

而不是 $this->var。

谢谢马格努斯。

于 2020-06-19T00:42:34.257 回答