在 PHP 中,我如何访问“:private"对象中的数组? [英] In PHP how can I access a ":private" array in an object?

查看:26
本文介绍了在 PHP 中,我如何访问“:private"对象中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WP_Admin_Bar 对象 中的大约 3.3beta1 项可以使用这种类型的语法访问,例如更改现有菜单项之一的 CSS 类:

Up until around 3.3beta1 items in the WP_Admin_Bar Object could be accessed using this type of syntax, for example to change the CSS class of one of the existing menu items:

$wp_admin_bar->menu->{'wp-logo'}['meta']['class'] = 'new-class';

运行 print_r($wp_admin_bar) 时,输出看起来像这样:

When running print_r($wp_admin_bar) the output looked something like this:

WP_Admin_Bar Object
(
    [menu] => stdClass Object
        (
            [my-account] => Array
                (

但是,在 3.3beta2 版本中,上述更改菜单项 CSS 类的语法不再有效,并且 print_r($wp_admin_bar) 的输出显示了不同的结构对于那个对象:

However, around version 3.3beta2 the above syntax for changing a menu item's CSS class no longer works, and the output from print_r($wp_admin_bar) reveals a different structure for that object:

WP_Admin_Bar Object
(
    [nodes:WP_Admin_Bar:private] => Array
        (
            [my-account] => stdClass Object
                (
                    [id] => my-account

                )

我意识到 Wordpress 可能不希望我以这种方式摆弄菜单,如果有更标准化的方式来做到这一点,我很乐意使用它,但据我所知,只有两个功能可用修改管理栏,add_menu_itemremove_menu_item,但这些并不能灵活地执行诸如更改现有菜单项的属性之类的操作.

I realize that Wordpress may not want me fiddling with the menus this way, and if there was a more standardized way to do this I would love to use it, but as far as I know there are only two functions that are available to modify the admin bar, add_menu_item and remove_menu_item, and these do not give the flexibility to do things like changing the attributes of existing menu items.

为了确认,我查看了 wp-includes/class-wp-admin-bar.php 很明显 Wordpress 已经改变了他们定义变量的方式.

To confirm, I looked at wp-includes/class-wp-admin-bar.php it is clear that Wordpress has changed the way they define the variables.

老班

class WP_Admin_Bar {
    var $menu;
    var $proto = 'http://';
    var $user;

新班级

class WP_Admin_Bar {
    private $nodes = array();
    private $root = array();

    public $proto = 'http://';
    public $user;

所以我的问题是,如果我可以访问 global $wp_admin_bar 对象,我是否可以访问 nodes:WP_Admin_Bar:private 中的对象?如果没有,是否还有其他方法可以访问这些对象,例如创建一个扩展 WP_Admin_Bar 类的新类,然后从那里访问这些对象?

So my question is if I have access to the global $wp_admin_bar object, is there I way I can access the objects inside nodes:WP_Admin_Bar:private? And if not, is there another way to get to these objects, such as creating a new class that extends the WP_Admin_Bar class and then accessing the objects from there?

ps:我正在尝试在不更改核心 Wordpress 文件的情况下克服这一挑战...

ps: I'm trying to overcome this challenge without changing the core Wordpress files...

文件链接:http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-includes/class-wp-admin-bar.php.source.html

推荐答案

将它们更改为受保护的成员变量并扩展类.

Change them to protected member variables and extend the class.

用私有成员编写类的人有效地使类最终".这表明您应该始终将成员写为受保护的,除非有非常非常好的理由不这样做.

Whoever wrote the class with private members effectively made the class "final". Which goes to show that you should always write your members as protected, unless there's a really, REALLY good reason to do otherwise.

希望有帮助...

这篇关于在 PHP 中,我如何访问“:private"对象中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆