从子类调用父属性而不使用parent :: __构造

时间:2014-08-20 17:26:38

标签: php oop parent-child

我有一个包含公共财产My_Admin

的父类$options

我有一个需要访问My_Notices属性的子类$options

如果在子课程中,我将parent::__construct()投入到子类的__construct()中,我可以访问$options 但是它会复制整个输出父类的。换句话说,我在同一页面上得到两个html页面输出,因为调用parent::_construct()的子类的实例化。

我已经尝试在我的子结构中声明$options,如public function __construct($options),但它告诉我:

Warning: Missing argument 1 for My_Notices::__construct()

**编辑**

以下是课程的细分:

class My_Admin
{
    private $sections;
    protected $settings;
    protected $defaults;
    public $options;
    public function __construct()
    {
        $this->settings = array();
        $this->get_settings();
        $this->defaults = array( /* stuff here */ );
        $this->sections = array( /* stuff here */ );
        add_filter('plugin_action_links', array($this, 'pluginpage'), 10, 2);
        add_action('admin_menu', array($this, 'menu'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue'));
        add_action('admin_init', array($this, 'deregister'), 20);
        add_action('wp_ajax_my_save', array($this, 'save'));
        if(!get_option('my_options')) $this->initialize();
        $this->options = get_option('my_options');
    }
}
class My_Notices extends My_Admin
{
    public function __construct()
    {
        add_action('admin_notices', array($this, 'baseconfig'));
        add_action('admin_init', array($this, 'baseignore'));
    }
    public function baseconfig(){
        global $pagenow;
        $uid = get_current_user_id();
        /* I NEED TO ACCESS $options HERE */
        if(!$this->options['base1'] || !$this->options['bs1name'])
        {
            if(!get_user_meta($uid, 'my_notice'))
            {   
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

根据您的意见,您需要在没有打印的情况下致电您的父母。您需要使用ob_startob_end_clean,但是您应该看看您的逻辑是否正确,因为如果父类打印文本不是最好的。

class My_Notices extends My_Admin {
      public __construct(){
          ob_start(); // prevents prints.
          parent::__construct();
          ob_end_clean(); // clear the capture

          // Your code here....

<强>已更新: 您也可以检查它是否是父项,然后打印:

class My_Admin
{
    private $sections;
    protected $settings;
    protected $defaults;
    public $options;
    public function __construct()
    {
        $this->settings = array();
        $this->get_settings();
        $this->defaults = array( /* stuff here */ );
        $this->sections = array( /* stuff here */ );

        if( !is_subclass_of($this, 'My_Admin' ) ) { // Is the parent
            add_filter('plugin_action_links', array($this, 'pluginpage'), 10, 2);
            add_action('admin_menu', array($this, 'menu'));
            add_action('admin_enqueue_scripts', array($this, 'enqueue'));
            add_action('admin_init', array($this, 'deregister'), 20);
            add_action('wp_ajax_my_save', array($this, 'save'));
       }

        if(!get_option('my_options')) $this->initialize();
        $this->options = get_option('my_options');
    }
    public function get_settings(){}
    public function initialize(){}
}
class My_Notices extends My_Admin
{
    public function __construct()
    {
        parent::__construct();
        add_action('admin_notices', array($this, 'baseconfig'));
        add_action('admin_init', array($this, 'baseignore'));

    }
}

了解如何运作:http://sandbox.onlinephpfunctions.com/code/e4ed5143244aaf0c57b29ff8487d911ab7cf99dd

答案 1 :(得分:-1)

为避免您的双重创作,这可能是您必须采取的路线:

将这些添加到My_Admin类:

制作private static $self;财产。

__construct()方法中添加self::$self = &$this;

制作方法:

public static getInstance(){
    return $self;
}
在My_Notices中

添加此选项,您需要访问options属性:

$my_admin = My_Admin::getInstance();
$my_admin->options;