1

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'))
            {   
            }
        }
    }
}
Works for a Living
  • 1,262
  • 2
  • 19
  • 44

2 Answers2

1

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

SnakeDrak
  • 3,406
  • 4
  • 28
  • 41
  • I like this idea but it still outputs duplicate content. :( – Works for a Living Aug 20 '14 at 17:47
  • If you use `ob_end_clean` the capture is deleted and finished. Can you do debug to search where is printing? – SnakeDrak Aug 20 '14 at 17:48
  • I don't really know. I did it exactly as you have above, but it still output duplicate html on the page. I will edit the OP with more info. – Works for a Living Aug 20 '14 at 17:52
  • I added a big chunk to the OP above. – Works for a Living Aug 20 '14 at 17:57
  • If you put my code before your `add_action` in My_Notices, is the page printed twice? If it's true then something o your `add_action` is printing twice the page. With `ob_start` and `ob_end_clean` nothing is showed. Test to put the `ob_end_clean` to end of your `script` and you will see nothing is showed. – SnakeDrak Aug 20 '14 at 18:05
  • I did this: `ob_start(); parent::__construct(); add_action('admin_notices', array($this, 'baseconfig')); add_action('admin_init', array($this, 'baseignore')); ob_end_clean();` And it still outputs duplicate content. – Works for a Living Aug 20 '14 at 18:08
  • So I think the solution I'm looking for is to be able to do it without parent::__construct(). Seems like I should be able to somehow. – Works for a Living Aug 20 '14 at 18:09
  • I have tried and it is working fine. The problem is not the `__construct()`. Test it here: http://sandbox.onlinephpfunctions.com/code/195a9e85e7f5198a3d813d0ff7719202ba592dc8. You can comment the `ob_end_clean()` line and the parent prints too. – SnakeDrak Aug 20 '14 at 18:13
  • Your sandbox showed me why. You are only instantiating the child class in the global space. I was instantiating the parent and the child. But that raises the question, what if I have a second class that extends My_Admin? I wouldn't be able to instantiate it and My_Notices without duplicating the parent output again. Seems counterintuitive. Maybe I should just avoid child/parent classes until I figure out what real benefit they have. – Works for a Living Aug 20 '14 at 18:19
  • Now I can instantiate both parent and child in the global space and access parent's options without any difficulty. Thanks. – Works for a Living Aug 20 '14 at 18:26
-1

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;
iam-decoder
  • 2,554
  • 1
  • 13
  • 28