2013-01-14 110 views
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 
    } 

} 
+0

只要調用方法!該方法是「public」,因此它在子類中可用! –

+0

'$ table-> yetAnotherMethod($ this-> myMethod())'; ?? – Sem

+0

@BenCarey'AnotherClass'不是'My_Plugin'的派生類。 –

回答

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

您應該將$somedata傳入您的函數調用。例如,

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

public function yetAnotherMethod($somedata){ 
    // do something ... 
} 
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); 
相關問題