【问题标题】:Call parent property from child class without parent::__construct从没有 parent::__construct 的子类调用父属性
【发布时间】:2014-08-20 17:26:38
【问题描述】:

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

我有一个子类 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'))
            {   
            }
        }
    }
}

【问题讨论】:

  • 为什么不喜欢class My_notices extends My_Admin {},然后只使用$this->options
  • 对不起,我真的没有看到任何真正的问题。不太确定您想要实现什么以及您的问题出在哪里。
  • 是的,我已经做到了。我认为这是在指定子类中假设的。
  • 父属性 $options 在 __construct() 之前声明并在 __construct() 中定义
  • 不完全。我没有在父 __construct() 中做任何回显,但它确实调用了将 html 回显到页面的方法。

标签: php oop parent-child


【解决方案1】:

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

【讨论】:

  • 我喜欢这个想法,但它仍然会输出重复的内容。 :(
  • 如果您使用ob_end_clean,捕获将被删除并完成。你能做调试来搜索打印在哪里吗?
  • 我真的不知道。我完全按照您上面的操作做了,但它仍然在页面上输出重复的 html。我将用更多信息编辑 OP。
  • 我在上面的 OP 中添加了一大块。
  • 如果您在 My_Notices 中将我的代码放在您的 add_action 之前,页面会打印两次吗?如果这是真的,那么您的add_action 正在打印两倍的页面。使用 ob_startob_end_clean 没有显示任何内容。测试将ob_end_clean 放在script 的末尾,您将看到没有显示任何内容。
【解决方案2】:

为了避免你的双重创造,这可能是你必须采取的路线:

将这些添加到您的 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;

【讨论】:

  • 但他不想要singleton...它是bad practice
  • 在这封信之后给我带来了一个致命错误:Fatal error: Access to undeclared static property: My_Admin::$self
猜你喜欢
  • 2012-01-30
  • 2012-05-20
  • 2014-08-06
  • 2017-03-07
  • 2010-12-08
  • 2016-04-05
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多