使用数组附加受保护的Object

时间:2015-11-18 21:35:24

标签: php arrays object

我有一个需要附加数组的受保护对象。对象看起来像这样:当我print_r($ cmb_team_members)时,我得到了这个;

CMB2 Object (
[cmb_id:protected] => team_member_metabox
[meta_box:protected] => Array
(
    [id] => team_member_metabox
    [title] => Team Member Metabox
    [type] => 
    [object_types] => Array
        (
            [0] => post_type_teammember
        )

    [context] => normal
    [priority] => high
    [show_names] => 1
    [show_on_cb] => 
    [show_on] => Array
        (
        )

    [cmb_styles] => 1
    [enqueue_js] => 1
    [fields] => Array
        (
            [_cmb2_division] => Array
                (
                    [name] => Division
                    [desc] => The division of the company this person works in (These can be edited in the Revolution Group theme options)
                    [id] => _cmb2_division
                    [type] => select
                    [options] => Array
                        (
                        )

                )

我想用数组追加$ cmb_team_members-> meta_box ['fields'] ['_ cmb2_division'] ['options']('a'=>'a','b'=>'b “)

我不能只是$ cmb_team_members-> meta_box ['fields'] ['_ cmb2_start_year'] ['options'] = array('a'=>'a','b'=>'b') ;它不像常规数组那样工作。我试图像这样扩展这个类:

class divisionClass extends CMB2
{       
    public function __set($name, $value)
    {
    /*Do something to append the Object with the array*/
    }
}

任何正确方向的推动都将受到高度赞赏。干杯!!!

1 个答案:

答案 0 :(得分:1)

在扩展类中创建一个新方法,该方法将接受您的选项并将它们附加到父类的protected属性。示例(根据您的具体情况调整):

class first_class
{

    protected $i_am_protected = array(
        'a' => 'b',
        'options' => array()
    );
}

class second_class extends first_class
{

    function setMyOptions($options)
    {
        $this->i_am_protected['options'] = $options;
    }
}

$second_class = new second_class();
$second_class->setMyOptions(array('foo' => 'bar'));