2013-01-14 2 views
0

Я пишу плагин WordPress, стиль ООП. Создание таблиц в интерфейсе администратора по-своему требует расширения другого класса.Включая класс в метод другого класса

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); 
Смежные вопросы