从空值创建默认对象(stdClass?)

时间:2015-08-06 20:35:47

标签: php function object default stdclass

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

  

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。

我还尝试使用新的stdClass()。但我也没有这样做。

1 个答案:

答案 0 :(得分:1)

您创建一个数组。

<button onclick="handleButton()"
    name="buttonLeft"
    type="button"
    class="myButton"
    id="ButtonLeft">
    Toggle Left Garage
</button>
<br />
Door is Secure

<script>
    function handleButton()
    {
        document.getElementById('ButtonLeft').innerHTML = "Left Garage Toggled";
    }

</script>

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

$this->groups = array();

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

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