【问题标题】:Creating default object from empty value ( stdClass? )从空值创建默认对象( stdClass? )
【发布时间】:2015-08-06 20:35:47
【问题描述】:

脚本运行良好,但只有当我提取用户组时,它才会说:

SteamUser.php (132): 从空值创建默认对象

    function getProfileData() {

    //Set Base URL for the query:
    if(empty($this->vanityURL)) {
        $base = "http://steamcommunity.com/profiles/{$this->userID}/?xml=1";
    } else {
        $base = "http://steamcommunity.com/id/{$this->vanityURL}/?xml=1";
    }

    try {
        $content = SteamUtility::fetchURL($base);
        if ($content) {
            $parsedData = new SimpleXMLElement($content);
        } else {
            return null;
        }
    } catch (Exception $e) {
        //echo "Whoops! Something went wrong!\n\nException Info:\n" . $e . "\n\n";
        return null;
    }

    if(!empty($parsedData)) {   
        $this->steamID64 = (string)$parsedData->steamID64;
        $this->steamID = (string)$parsedData->steamID;
        $this->stateMessage = (string)$parsedData->stateMessage;
        $this->visibilityState = (int)$parsedData->visibilityState;
        $this->privacyState = (string)$parsedData->privacyState;

        $this->avatarIcon = (string)$parsedData->avatarIcon;
        $this->avatarMedium = (string)$parsedData->avatarMedium;
        $this->avatarFull = (string)$parsedData->avatarFull;

        $this->vacBanned = (int)$parsedData->vacBanned;
        $this->tradeBanState = (string)$parsedData->tradeBanState;
        $this->isLimitedAccount = (string)$parsedData->isLimitedAccount;

        $this->onlineState = (string)$parsedData->onlineState;
        $this->inGameServerIP = (string)$parsedData->inGameServerIP;

        //If their account is public, get that info:
        if($this->privacyState == "public") {
            $this->customURL = (string)$parsedData->customURL;
            $this->memberSince = (string)$parsedData->memberSince;

            $this->steamRating = (float)$parsedData->steamRating;
            $this->hoursPlayed2Wk = (float)$parsedData->hoursPlayed2Wk;

            $this->headline = (string)$parsedData->headline;
            $this->location = (string)$parsedData->location;
            $this->realname = (string)$parsedData->realname;
            $this->summary = (string)$parsedData->summary;
        }

        //If they're in a game, grab that info:
        if($this->onlineState == "in-game") {
            $this->inGameInfo = array();
            $this->inGameInfo["gameName"] = (string)$parsedData->inGameInfo->gameName;
            $this->inGameInfo["gameLink"] = (string)$parsedData->inGameInfo->gameLink;
            $this->inGameInfo["gameIcon"] = (string)$parsedData->inGameInfo->gameIcon;
            $this->inGameInfo["gameLogo"] = (string)$parsedData->inGameInfo->gameLogo;
            $this->inGameInfo["gameLogoSmall"] = (string)$parsedData->inGameInfo->gameLogoSmall;
        } 

        //Any weblinks listed in their profile:
        if(!empty($parsedData->weblinks)) {
            $this->weblinks = array();

            $i = 0;
            foreach ($parsedData->weblinks->weblink as $weblink) {
                $this->weblinks[$i]->title = (string)$weblink->title;
                $this->weblinks[$i]->link = (string)$weblink->link;
                $i++;
            }
        }   

  if(!empty($parsedData->groups)) {
            $this->groups = array();

            $i = 0;
            foreach ($parsedData->groups->group as $group) {

$this->groups[$i]->groupID64 = (string)$group->groupID64; // 脚本中的第 132 行

                $this->groups[$i]->groupName = (string)$group->groupName;
                $this->groups[$i]->groupURL = (string)$group->groupURL;
                $this->groups[$i]->headline = (string)$group->headline;
                $this->groups[$i]->summary = (string)$group->summary;

                $this->groups[$i]->avatarIcon = (string)$group->avatarIcon;
                $this->groups[$i]->avatarMedium = (string)$group->avatarMedium;
                $this->groups[$i]->avatarFull = (string)$group->avatarFull;

                $this->groups[$i]->memberCount = (string)$group->memberCount;
                $this->groups[$i]->membersInChat = (string)$group->membersInChat;
                $this->groups[$i]->membersInGame = (string)$group->membersInGame;
                $this->groups[$i]->membersOnline = (string)$group->membersOnline; 

                $i++;
            }

        }                    

    }
}
  .

。 . 我已经尝试过使用关联数组,但是没有用;也许做这一切都是新的。 -_-

所以,我根本无法设置 error_reporting(0) 或 ini_set。

我也尝试使用 new stdClass()。但我也没有让它运行。

【问题讨论】:

  • set error_reporting(0) 没有解决任何问题。它隐藏了问题。但问题仍然潜伏在那里没有解决。您永远不需要这样做(实际上您应该尝试相反的情况并启用尽可能多的警告报告)
  • 我写了一个答案。有帮助吗?您需要更多帮助吗?你能解决这个问题吗?
  • 对不起!是的,效果很好!非常感谢你! :)

标签: php function object default stdclass


【解决方案1】:

你创建一个数组。

$this->groups = array();

但是在这个数组中你没有创建任何对象。您只需将此数组的元素(未初始化)用作对象。您应该在 132. 行之前添加此行。

$this->groups[$i] = new Group();

或类似的东西。 如果您没有 Group 课程,您应该尝试

$this->groups[$i] = new stdClass();

【讨论】:

    猜你喜欢
    • 2020-04-09
    • 1970-01-01
    • 2015-01-28
    • 2017-11-29
    • 2017-09-30
    • 2016-08-18
    • 2018-07-18
    • 2019-03-14
    相关资源
    最近更新 更多