【问题标题】:PHP, How maintain parent class data into child class?PHP,如何将父类数据维护到子类中?
【发布时间】:2016-03-08 05:16:01
【问题描述】:

所以,我终于找到了一种方法来影响所需的功能。

我试图修改的函数包含在一个类中,所以我做了这样的事情:

class my_Check extends Appointments {

   function __construct() {
      $this->unregister_parent_hook();
      add_action( 'wp_ajax_post_confirmation', array( $this, 'post_confirmation' ) );
      add_action( 'wp_ajax_nopriv_post_confirmation', array( $this, 'post_confirmation' ) );
   }

   function unregister_parent_hook() {
      global $appointments;  //this was the object created with the parent class
      remove_action( 'wp_ajax_post_confirmation', array( $appointments, 'post_confirmation' ) );
      remove_action( 'wp_ajax_nopriv_post_confirmation', array( $appointments, 'post_confirmation' ) );
   }

   function post_confirmation() {

        ...do the stuff with my mods...

   }
}

$new_Check = new my_Check();

只是,我现在有一个新问题。父类在__construct() 中做了很多事情(很多add_action()'s 等。而$this 填充了很多数据)。问题是,这些其他的东西和数据似乎并没有转移到子类中。我尝试在孩子的__construct() 函数中添加parent::__construct(),但这似乎不起作用。

除了需要从父类继承的 $this 中的更多数据之外,我的 mod 的代码可以正常工作。

如何将所有父类的变量、函数、钩子和过滤器等维护到子类中?

而且,我不能真正使用父类更改文件,因为它位于插件核心文件中,我不想直接修改。

【问题讨论】:

  • 我们能看到 parent::__construct 方法吗?它是否需要将任何参数传递给它?事实上,你能发布整个父类吗?
  • 由于整个父类有几千行,这里是父类文件的链接:mokyoworks.com/appointments.zip

标签: php wordpress class plugins extends


【解决方案1】:

我见过很多例子,人们使用parent::__construct() 来传递父级的变量等,他们把它放在子级的__construct() 函数中。这对我不起作用。

但是,我终于能够通过在添加到父类的新函数中调用父构造函数来使其工作。像这样:

function post_confirmation() {

    parent::__construct();

    ...do the stuff with my mods...
}

从这篇文章中得到了解决方案> https://stackoverflow.com/a/32232406/1848815

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-14
    • 2022-12-21
    • 2011-11-13
    • 2010-11-27
    • 2018-12-05
    • 1970-01-01
    相关资源
    最近更新 更多