0

我正在写一个 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
    }

}
4

3 回答 3

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);
于 2013-01-14T14:25:23.397 回答
1

你应该传入$somedata你的函数调用。例如

$table->yetAnotherMethod($this->myMethod());

public function yetAnotherMethod($somedata){
    // do something ...
}
于 2013-01-14T14:26:28.793 回答
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);
于 2013-01-14T14:32:04.793 回答