stdClass 检测 isset、empty、other 时出现问题

发布于 2024-11-18 16:42:11 字数 4186 浏览 2 评论 0原文

首先我要说的是我正在使用 CodeIgniter 和 Elliot Haughin 创建的 Facebook 类。 http://www.haughin.com/code/facebook/ 我有工作发现一切都很好。然而,当我使用各种 FB 帐户测试我的代码时,我偶然发现了一个我想评估的问题,但我的尝试失败了。我用谷歌搜索并访问了其他论坛,并且不断得出我最初想到的答案。无论如何应该起作用的东西,但在这种情况下似乎不起作用..

既然我已经说过了,我的问题是.. FB 传回的对象并不总是有一个特定的条目。为了这个例子,我会说用户名..

这是一个使用用户名返回的数据的示例:

facebookResponse Object
(
    [__construct:private] => 
    [__resp] => stdClass Object
        (
            [data] => stdClass Object
                (
                    [id] => 0000000000000
                    [name] => Random.User
                    [first_name] => Random
                    [middle_name] => ...
                    [last_name] => User
                    [link] => http://www.facebook.com/profile.php?id=000000000
                    [birthday] => 11/26/1980
                    [gender] => male
                    [timezone] => -4
                    [locale] => en_US
                    [verified] => 1
                    [updated_time] => 2011-05-30T19:24:53+0000
                )

这是一个没有用户名的示例:

facebookResponse Object
(
    [__construct:private] => 
    [__resp] => stdClass Object
        (
            [data] => stdClass Object
                (
                    [id] => 000000000000000000000
                    [name] => Random User
                    [first_name] => Random
                    [last_name] => User
                    [link] => http://www.facebook.com/Random.User
                    [username] => Random.User
                    [birthday] => 11/10/1985
                    [hometown] => stdClass Object
                        (
                            [id] => 000000000000000
                            [name] => Coventry, Connecticut
                        )

                    [location] => stdClass Object
                        (
                            [id] => 00000000000000
                            [name] => San Jose, California
                        )

我上次尝试解决问题失败的尝试类似于..

        $fb_result = $this->facebook->call('get', 'me', array('metadata' => 1));
        $fbFirstName = $fb_result->first_name;
        $fbLastName = $fb_result->last_name;
        $fbUserName = $fb_result->username;
        $fbUserId = $fb_result->id;
        $fbHomeTown = $fb_result->hometown->name;
        $fbLocation = $fb_result->location->name;
        echo "<strong>First Name: </strong>"; if((isset($fbFirstName))AND(!empty($fbFirstName))AND(trim($fbFirstName) !== "")){echo $fb_result->first_name ."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>Last Name: </strong>"; if((isset($fbLastName))AND(!empty($fbLastName))AND(trim($fbLastName) !== "")){echo $fb_result->last_name."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>Username: </strong>"; if((isset($fbUserName))AND(!empty($fbUserName))AND(trim($fbUserName) !== "")){echo $fb_result->username."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>User FB ID: </strong>"; if((isset($fbUserId))AND(!empty($fbUserId))AND(trim($fbUserId) !== "")){echo $fb_result->id."<br />"; }else{ echo "Not found.<br />"; }   
        echo "<strong>Location hometown: </strong>"; if((isset($fbHomeTown))AND(!empty($fbHomeTown))AND(trim($fbHomeTown) !== "")){echo $fb_result->hometown->name."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>Location current (manual input): </strong>"; if((isset($fbLocation))AND(!empty($fbLocation))AND(trim($fbLocation) !== "")){echo $fb_result->location->name."<br />"; }else{ echo "Not found.<br />"; }

它在哪里做我首先将其定义为变量,然后使用 if-else 检查它,但是现在是否有效。我现在收到“尝试获取非对象的属性”错误。这意味着我想因为它不存在并且我试图将它设置为变量,无论它会返回一个错误。我昨天和昨天花了大部分时间试图解决这个问题,但我不得不说我陷入了困境。 php错误类型只不过是一个“通知”,但我通常不喜欢让我的代码处于这样的状态,即使是像这样的错误也会踢回来..这就是说我迷路了..

First off let me start by saying I am using CodeIgniter and the Facebook Class Elliot Haughin created. http://www.haughin.com/code/facebook/ Which I have that working find all all is well. However when I was testing my code with various FB accounts I stumbled across an issue I'd like to assess but my attempts have failed. I have googled around and hit up other forums and I consistently come up with the answers I originally came up with. Something that should by all means work but doesn't seem to in this case..

Now that I have said that, my issue is.. The object that FB is passing back doesn't always have a particular entry in it. For the sake of this example Ill say the username..

Heres an example of the Data returned with a username:

facebookResponse Object
(
    [__construct:private] => 
    [__resp] => stdClass Object
        (
            [data] => stdClass Object
                (
                    [id] => 0000000000000
                    [name] => Random.User
                    [first_name] => Random
                    [middle_name] => ...
                    [last_name] => User
                    [link] => http://www.facebook.com/profile.php?id=000000000
                    [birthday] => 11/26/1980
                    [gender] => male
                    [timezone] => -4
                    [locale] => en_US
                    [verified] => 1
                    [updated_time] => 2011-05-30T19:24:53+0000
                )

Here is an example without a username:

facebookResponse Object
(
    [__construct:private] => 
    [__resp] => stdClass Object
        (
            [data] => stdClass Object
                (
                    [id] => 000000000000000000000
                    [name] => Random User
                    [first_name] => Random
                    [last_name] => User
                    [link] => http://www.facebook.com/Random.User
                    [username] => Random.User
                    [birthday] => 11/10/1985
                    [hometown] => stdClass Object
                        (
                            [id] => 000000000000000
                            [name] => Coventry, Connecticut
                        )

                    [location] => stdClass Object
                        (
                            [id] => 00000000000000
                            [name] => San Jose, California
                        )

my last failed attempt at trying to trouble shoot this goes something like..

        $fb_result = $this->facebook->call('get', 'me', array('metadata' => 1));
        $fbFirstName = $fb_result->first_name;
        $fbLastName = $fb_result->last_name;
        $fbUserName = $fb_result->username;
        $fbUserId = $fb_result->id;
        $fbHomeTown = $fb_result->hometown->name;
        $fbLocation = $fb_result->location->name;
        echo "<strong>First Name: </strong>"; if((isset($fbFirstName))AND(!empty($fbFirstName))AND(trim($fbFirstName) !== "")){echo $fb_result->first_name ."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>Last Name: </strong>"; if((isset($fbLastName))AND(!empty($fbLastName))AND(trim($fbLastName) !== "")){echo $fb_result->last_name."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>Username: </strong>"; if((isset($fbUserName))AND(!empty($fbUserName))AND(trim($fbUserName) !== "")){echo $fb_result->username."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>User FB ID: </strong>"; if((isset($fbUserId))AND(!empty($fbUserId))AND(trim($fbUserId) !== "")){echo $fb_result->id."<br />"; }else{ echo "Not found.<br />"; }   
        echo "<strong>Location hometown: </strong>"; if((isset($fbHomeTown))AND(!empty($fbHomeTown))AND(trim($fbHomeTown) !== "")){echo $fb_result->hometown->name."<br />"; }else{ echo "Not found.<br />"; }
        echo "<strong>Location current (manual input): </strong>"; if((isset($fbLocation))AND(!empty($fbLocation))AND(trim($fbLocation) !== "")){echo $fb_result->location->name."<br />"; }else{ echo "Not found.<br />"; }

Which doing it where I am defining it as a variable first then checking it with if-else now is working however. I am now getting "Trying to get property of non-object" for an error. Which means I suppose that since its not there and I am trying to set it as a variable regardless it kicks back an error for it. I have spent the better part of the day and yesterday trying to resolve this and I have to say I am stuck. The php error type is nothing more then a mere "Notice" but I don't generally like to leave my code in a status that will kick even so much as an error like that back.. That said I am lost..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-11-25 16:42:11

您没有说,但这听起来像是针对

$fbHomeTown = $fb_result->hometown->name;

诸如 hometown 属性不存在等行给出的错误。

防止出现此通知的方法(并且您绝对应该争取完全无错误的代码)是执行以下操作:

$fbHomeTown = isset($fb_result->hometown->name) ? trim($fb_result->hometown->name) : null;

这将您的临时变量设置为已知的安全值(修剪后的值或 null )。然后,您可以通过执行以下操作来显式检查没有价值的情况,

if($fbHomeTown === null) ...  // 3 equal signs

但是,在很多情况下(包括您的情况),您甚至不需要区分“不存在”和“它是一个空字符串”和您将能够轻松完成处理,例如

echo "<strong>First Name: </strong>".
     (empty($fbFirstName) ? "Not found.<br />" : $fbFirstName);

You don't say, but this sounds like the error is given for the lines like

$fbHomeTown = $fb_result->hometown->name;

when e.g. the hometown property does not exist.

The way to prevent this notice (and you should definitely strive for completely error-free code) is to do something like this:

$fbHomeTown = isset($fb_result->hometown->name) ? trim($fb_result->hometown->name) : null;

This sets your temporary variable to a known safe value (either the trimmed value or null). You can then check explicitly for the case where there was no value to begin with by doing

if($fbHomeTown === null) ...  // 3 equal signs

However, in lots of cases (including yours) you would not even need to distinguish between the "did not exist" and "it's an empty string" and you would be able to easily complete the processing with something like

echo "<strong>First Name: </strong>".
     (empty($fbFirstName) ? "Not found.<br />" : $fbFirstName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文