简体   繁体   中英

Call parent property from child class without parent::__construct

I've got a parent class My_Admin with a public property $options

I've got a child class My_Notices that needs to access the $options property.

If, in the child class, I throw parent::__construct() into the child class's __construct() , I am able to access $options BUT it duplicates the entire output of the parent class. In other words, I'm getting two html page outputs on the same page, because of the instantiation of the child class calling parent::_construct() .

I've tried declaring $options in my child construct like public function __construct($options) but then it tells me:

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

** EDIT **

Here's a breakdown of the classes:

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

When, according to your comments you need call your parent's without print. You need use ob_start and ob_end_clean , but you should see if your logic is correct because if the parent's class prints text is not the best.

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

          // Your code here....

UPDATED : Also you can check if it is the parent and then print:

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

    }
}

See how works: http://sandbox.onlinephpfunctions.com/code/e4ed5143244aaf0c57b29ff8487d911ab7cf99dd

To avoid your double creations, this might be the route you have to take:

Add these to your My_Admin class:

make a private static $self; property.

in your __construct() method add self::$self = &$this;

make a method:

public static getInstance(){
    return $self;
}

in My_Notices add this where you need to access the options property:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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