用数组附加受保护的对象


Append a protected Object with an array

我有一个受保护的对象,我需要向其附加一个数组。对象看起来是这样的:我在打印_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']=数组('a'=>'a','b'=>'b');它不像常规数组那样工作。我试着这样扩展类:

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

如能朝着正确的方向努力,我们将不胜感激。干杯

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

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'));