从没有parent::__construct的子类调用parent属性


Call parent property from child class without parent::__construct

我有一个父类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()

** EDIT **

下面是类的分类:

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'))
            {   
            }
        }
    }
}

什么时候,根据你的评论,你需要打电话给你的父母而不需要打印。你需要使用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

为了避免重复创建,您可能必须采用以下路径:

将这些添加到My_Admin类中:

创建private static $self;属性

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

创建方法:

public static getInstance(){
    return $self;
}

在my_notifications中,在需要访问options属性的地方添加这个:

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