drep94225 2017-12-01 03:05
浏览 61
已采纳

为什么print_r没有显示WP_User对象的所有属性?

If I do this I get many properties shown, including display_name, but the first and last names are NOT shown

$user = get_userdata( 4 );
print_r( $user );

However they clearly exist because if I immediately afterwards do this, I am shown the proper last name. The Wordpress docs also mention last_name as being a property.

echo $user->last_name;

So why doesn't print_r show all of the properties? This casts a lot of doubt on being able to use print_r to discover information.

  • 写回答

1条回答 默认 最新

  • dqpfkzu360216 2017-12-01 03:33
    关注

    last_name is not a real property of WP_User, but it's made available via magic methods for backwards compatibility.

    It's listed as a public property in the docs, but this is a little misleading. More accurately, you can access it as a public property. But when you look at the code, it's being retrieved and set using magic methods.

    Proof from the Code

    Here's an excerpt of the most relevant code, showing how Wordpress actually keeps a list of several of these backward-compatibility properties, including last_name, in a private, static property called back_compat_keys. When the user requests one of these properties, the magic method __get is called. The magic method uses get_user_meta() to actually retrieve the data for that property. In other words, the data isn't actually stored in the WP_User object; Wordpress just lets you pretend that it is, and it only fetches it when explicitly requested. Here's the code:

    <?php
    class WP_User {
        // ...
            /**
             * @static
             * @since 3.3.0
             * @access private
             * @var array
             */
            private static $back_compat_keys;
    
            public function __construct( $id = 0, $name = '', $blog_id = '' ) {
                    if ( ! isset( self::$back_compat_keys ) ) {
                            $prefix = $GLOBALS['wpdb']->prefix;
                            self::$back_compat_keys = array(
                                    'user_firstname' => 'first_name',
                                    'user_lastname' => 'last_name',
                                    'user_description' => 'description',
                                    'user_level' => $prefix . 'user_level',
                                    $prefix . 'usersettings' => $prefix . 'user-settings',
                                    $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
                            );
                    }
    
                    // ...
            }
    
            // ...
    
            /**
             * Magic method for accessing custom fields.
             *
             * @since 3.3.0
             * @access public
             *
             * @param string $key User meta key to retrieve.
             * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID.
             */
            public function __get( $key ) {
                    // ...
    
                    if ( isset( $this->data->$key ) ) {
                            $value = $this->data->$key;
                    } else {
                            if ( isset( self::$back_compat_keys[ $key ] ) )
                                    $key = self::$back_compat_keys[ $key ];
                            $value = get_user_meta( $this->ID, $key, true );
                    }
    
                    // ...
    
                    return $value;
            }
    
            /**
             * Magic method for setting custom user fields.
             *
             * This method does not update custom fields in the database. It only stores
             * the value on the WP_User instance.
             *
             * @since 3.3.0
             * @access public
             *
             * @param string $key   User meta key.
             * @param mixed  $value User meta value.
             */
            public function __set( $key, $value ) {
                    if ( 'id' == $key ) {
                            _deprecated_argument( 'WP_User->id', '2.1.0',
                                    sprintf(
                                            /* translators: %s: WP_User->ID */
                                            __( 'Use %s instead.' ),
                                            '<code>WP_User->ID</code>'
                                    )
                            );
                            $this->ID = $value;
                            return;
                    }
    
                    $this->data->$key = $value;
            }
    
            /**
             * Magic method for unsetting a certain custom field.
             *
             * @since 4.4.0
             * @access public
             *
             * @param string $key User meta key to unset.
             */
            public function __unset( $key ) {
                    // ...
    
                    if ( isset( $this->data->$key ) ) {
                            unset( $this->data->$key );
                    }
    
                    if ( isset( self::$back_compat_keys[ $key ] ) ) {
                            unset( self::$back_compat_keys[ $key ] );
                    }
            }
    
            // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合