【发布时间】: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