Php 如何将一个函数的修改变量转换为另一个函数?
如何将一个函数的变量传递给另一个函数?例如:Php 如何将一个函数的修改变量转换为另一个函数?,php,oop,Php,Oop,如何将一个函数的变量传递给另一个函数?例如: <?php class getUserSummaries { protected $userNick; function func1($userNick) { $getUser = file_get_contents('http://example.com/api/' . $userNick . '/summarie/'); $getUser = json_decode($getUser, t
<?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];
}
}
这个问题有点模棱两可,但我将假设func1
将被调用,然后由一些外部代码调用func2
。这可能会发生多次。在这种情况下,设置一个类变量是有意义的:
protected $userId;
并将最后一行添加到func1
:
$this->userId = $getUser;
然后将func2
的第一行更改为
$getR = file_get_contents("http://example.com/api2/replays/".$this->userId."/");
如果假设不是您想要的,请告知。如果您将其设置为$this->getUser
,func2i将可以看到它,但workwogsland没有给出更具体的示例below@DanielMartínMuñoz我保留了你的最后一行逻辑,因为它构成了语法错误,我假设您应该在年、月和日周围有字符串,但留给您添加。另外,值得注意的是,$getUser
在第二个函数中没有定义,可能应该是$var