Php 从没有父类的子类调用父属性::\u构造
我有一个带有公共属性的父类Php 从没有父类的子类调用父属性::\u构造,php,oop,parent-child,Php,Oop,Parent Child,我有一个带有公共属性的父类My_Admin,$options 我有一个子类My_Notices,需要访问$options属性 如果在子类中,我将parent::\uu-construct()放入子类的\uu-construct(),我可以访问$options,但它复制了父类的整个输出。换句话说,我在同一个页面上得到两个html页面输出,因为子类的实例化调用了parent::_construct() 我尝试在我的子结构中声明$options,比如公共函数uu\u结构($options),但它告诉我
My_Admin
,$options
我有一个子类My_Notices
,需要访问$options
属性
如果在子类中,我将parent::\uu-construct()
放入子类的\uu-construct()
,我可以访问$options
,但它复制了父类的整个输出。换句话说,我在同一个页面上得到两个html页面输出,因为子类的实例化调用了parent::_construct()
我尝试在我的子结构中声明$options
,比如公共函数uu\u结构($options)
,但它告诉我:
警告:My_通知缺少参数1::_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'))
{
}
}
}
}
为了避免双重创造,这可能是您必须采取的路线: 将以下内容添加到My_Admin类: 制作一个
私有静态$self代码>属性
在\u construct()
方法中添加self::$self=&$this代码>
制定一个方法:
public static getInstance(){
return $self;
}
在My_通知中,在需要访问options属性的位置添加以下内容:
$my_admin = My_Admin::getInstance();
$my_admin->options;
根据你的意见,你需要打电话给你父母,但不打印。您需要使用和,但您应该查看您的逻辑是否正确,因为如果父类的类打印文本不是最好的
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'));
}
}
查看工作原理:为什么不喜欢类My_notices扩展My_Admin{}
,然后只使用$this->options
?抱歉,我没有看到任何真正的问题。我不太清楚你想要实现什么,你的问题在哪里。是的,我已经做到了。我想这是在指定child class.parent属性$options之前声明的,而在\uuuu construct()中定义的并不完全是。我在parent _construct()中不做任何回显,但它会调用将html回显到页面的方法。。。这是一个糟糕的做法
。严格遵守这一点也让我犯了一个致命错误:致命错误:访问未声明的静态属性:My_Admin::$self
我喜欢这个想法,但它仍然输出重复的内容(如果您使用ob\u end\u clean
捕获将被删除并完成。您可以进行调试以搜索打印位置吗?我真的不知道。我完全按照上面的操作进行,但它仍然会在页面上输出重复的html。我将使用更多信息编辑OP。我在上面的OP中添加了一大块内容。如果您将我的代码放在添加操作之前
在我的通知中,页面是否打印了两次?如果是真的,那么您的add\u action
将打印两次页面。使用ob\u start
和ob\u end\u clean
时,没有显示任何内容。测试将ob\u end\u clean
放在脚本的末尾,您将看到没有显示任何内容。