繁体   English   中英

从没有父类的子类中调用父类属性:____ struct

[英]Call parent property from child class without parent::__construct

我有一个带有公共属性$options的父类My_Admin

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

如果在子类中将parent::__construct()放入子类的__construct() ,则我可以访问$options 它会复制父类的整个输出。 换句话说,由于子类调用parent::_construct()的实例化,我在同一页面上获得了两个html页面输出。

我试过在子结构中声明$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'))
            {   
            }
        }
    }
}

何时,根据您的意见,您需要致电父母的无印刷品。 您需要使用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_Notices中,将其添加到需要访问options属性的位置:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM