Php函数错误问题

时间:2013-04-22 作者:SoupOfStars

嗨,我好像收到这个消息了

Warning: Creating default object from empty value in C:\\xampp\\htdocs\\wp-content\\themes\\knowledgepress\\lib\\ticket-functions.php on line 851
然而,看看代码,我看不出有什么问题?

if (!function_exists(\'woo_supportpress_get_ticket_details\')) {
    function woo_supportpress_get_ticket_details( $post_id ) { 

        $status = current(wp_get_object_terms( $post_id, \'ticket_status\' ));
        $type = current(wp_get_object_terms( $post_id, \'ticket_type\' ));
        $priority = current(wp_get_object_terms( $post_id, \'ticket_priority\' ));

        if (!isset($status->name)) $status = \'\';

        if (!isset($type->name)) $type = \'\';

        if (!isset($priority->name)) $priority = \'\';

        $ticket_details = array(
            \'status\' => $status
            ,\'type\' => $type
            ,\'priority\' => $priority
            ,\'assigned_to\' => get_user_by(\'id\', get_post_meta( $post_id, \'_responsible\', true))
            ,\'reported_by\' => get_post_meta( $post_id, \'_reported_by\', true)
            ,\'reported_by_email\' => get_post_meta( $post_id, \'_reported_by_email\', true)
        );

        /* Support \'Anybody\' user */
        if (!isset($ticket_details[\'assigned_to\']->display_name)) $ticket_details[\'assigned_to\']->display_name = \'Anybody\';
        if (!isset($ticket_details[\'assigned_to\']->ID)) $ticket_details[\'assigned_to\']->ID = 0;

        return $ticket_details;

    }
}
第851行:

if (!isset($ticket_details[\'assigned_to\']->display_name)) $ticket_details[\'assigned_to\']->display_name = \'Anybody\'; \'

1 个回复
SO网友:fuxia

什么时候$ticket_details[\'assigned_to\']->display_name 未设置,get_user_by() 返回的不是对象,而是FALSE.

如果有对象,请进行第一次测试:

$user = get_user_by(\'id\', get_post_meta( $post_id, \'_responsible\', true));

if ( ! is_object( $user ) )
    return;

$ticket_details[\'assigned_to\'] = $user;
display_name 如果存在用户对象,则始终设置。至少为空字符串。

结束