我正在编写一个WordPress插件,OOP风格。 在管理界面中以本机方式创建表需要扩展另一个类。
myPlugin.php:
class My_Plugin {
public function myMethod(){
return $somedata;
}
public function anotherMethod(){
require_once('anotherClass.php');
$table = new AnotherClass;
$table->yetAnotherMethod();
}
}
anotherClass.php:
class AnotherClass extends WP_List_Table {
public function yetAnotherMethod(){
// how do I get the returned data $somedata here from the method above?
// is there a way?
// ... more code here ...
// table is printed to the output buffer
}
}
答案 0 :(得分:1)
由于myMethod()
不是静态的,您需要My_Plugin
的(?)实例来获取该信息:
$myplugin = new My_Plugin();
....
$data = $myplugin->myMethod();
或者,您将该信息提供给yetAnotherMothod
来电:
$data = $this->myMethod();
require_once('anotherClass.php');
$table = new AnotherClass;
$table->yetAnotherMethod($data);
答案 1 :(得分:1)
您应该将$somedata
传递给您的函数调用。例如
$table->yetAnotherMethod($this->myMethod());
public function yetAnotherMethod($somedata){
// do something ...
}
答案 2 :(得分:0)
您myMethod()
方法是公开的,因此可以在任何地方访问。确保包含所有必要的文件,如下所示:
require_once('myPlugin.php')
require_once('anotherClass.php')
然后简单地写下这样的东西:
// Initiate the plugin
$plugin = new My_Plugin;
// Get some data
$data = $plugin->myMethod();
// Initiate the table object
$table = new AnotherClass;
// Call the method with the data passed in as a parameter
$table->yetAnotherMethod($data);