PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-为什么print_r不显示WP_User对象的所有属性?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如果执行此操作,则会显示很多属性,包括display_name,但不会显示名字和姓氏

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

但是,它们显然存在,因为如果我此后立即执行此操作,则会看到正确的姓氏. wordpress文档还提到last_name一个属性.

echo $user->last_name;

那么为什么print_r不显示所有属性?对于是否可以使用print_r来发现信息,这产生了很多疑问.

解决方法:

last_name不是WP_User的真实属性,但可以通过魔术方法将其提供以实现向后兼容.

它被列为公共财产in the docs,但这有点误导.更准确地说,您可以将其作为公共财产使用.但是当您查看@L_801_13@时,将使用魔术方法对其进行检索和设置.

守则证明

这是最相关的代码的摘录,显示wordpress如何实际上在称为BACk_compat_keys的私有静态属性中保留了这些向后兼容属性(包括last_name)的列表.当用户请求这些属性之一时,魔术方法__get被调用. magic方法使用get_user_Meta()来实际检索该属性的数据.换句话说,数据实际上并没有存储在WP_User对象中. wordpress只是让您假装它是,只有在明确请求时才获取它.这是代码

<?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 ] );
                }
        }

        // ...
}

大佬总结

以上是大佬教程为你收集整理的php-为什么print_r不显示WP_User对象的所有属性?全部内容,希望文章能够帮你解决php-为什么print_r不显示WP_User对象的所有属性?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: