如何将函数的修改变量转换为另一个函数?

时间:2016-01-11 01:37:58

标签: php oop

如何将函数的变量传递给另一个函数?例如:

<?php
class getUserSummaries {

    protected $userNick;

    function func1($userNick) {
        $getUser = file_get_contents('http://example.com/api/' . $userNick . '/summarie/');
        $getUser = json_decode($getUser, true);
        $getUser = $getUser['userid'];
    }

    function func2($var) {
        $getR = file_get_contents('http://example.com/api2/replays/' . $getUser . '/');
        $getR = json_decode($getR, true);
        $getR = $getR[year][month][day][19999];
    }
}

我想将$getUser从第一个函数(func1)传递到第二个函数(func2)。

1 个答案:

答案 0 :(得分:2)

这个问题有点含糊不清,但我会回答假设func1将被调用,然后稍后func2被一些外部代码调用。这可能会发生多次。在这种情况下,设置一个类变量是有意义的:

protected $userId;

并将最后一行添加到func1

$this->userId = $getUser;

然后将func2的第一行更改为

$getR = file_get_contents("http://example.com/api2/replays/".$this->userId."/");

请告知假设不是您想要的。