如何将一个函数的变量传递给另一个函数?例如:
<?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
)。
这个问题有点模棱两可,但我会假设func1
将被一些外部代码调用,然后func2
将被调用。这种情况可能会发生多次。在这种情况下,设置一个类变量是有意义的:
protected $userId;
并将最后一行添加到func1
:
$this->userId = $getUser;
然后将func2
的第一行更改为
$getR = file_get_contents("http://example.com/api2/replays/".$this->userId."/");
如果假设不是你想要的,请给出建议。