【问题标题】:Append a protected Object with an array用数组附加受保护的对象
【发布时间】:2015-11-18 21:35:24
【问题描述】:

我有一个受保护的对象,我需要将一个数组附加到该对象。对象看起来像这样:我在 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*/
    }
}

我们将不胜感激任何朝着正确方向的推动。干杯!!!

【问题讨论】:

    标签: php arrays object


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。这帮助了很多。
    猜你喜欢
    • 2012-05-28
    • 2014-08-10
    • 2014-10-15
    • 1970-01-01
    • 2013-01-22
    • 2015-03-30
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    相关资源
    最近更新 更多