0

I'm writing a WordPress plugin, OOP-style. Creating tables in the admin interface the native way requires extending another class.

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
  • Just call the method! The method is public, therefore it is available in the sub class! Commented Jan 14, 2013 at 14:25
  • $table->yetAnotherMethod($this->myMethod()); ?? Commented Jan 14, 2013 at 14:26
  • @BenCarey AnotherClass is not a derived class of My_Plugin Commented Jan 14, 2013 at 14:32
  • @BartFriederichs I noticed this and corrected it in my answer. The question is not worded particularly well! Commented Jan 14, 2013 at 14:33

3 Answers 3

1

As myMethod() isn't static, you'll need an (the?) instance of My_Plugin to get that info:

 $myplugin = new My_Plugin(); .... $data = $myplugin->myMethod(); 

Or, you supply that info to the yetAnotherMothod call:

 $data = $this->myMethod(); require_once('anotherClass.php'); $table = new AnotherClass; $table->yetAnotherMethod($data); 
Sign up to request clarification or add additional context in comments.

Comments

1

You should pass $somedata into your function call. For example

$table->yetAnotherMethod($this->myMethod()); public function yetAnotherMethod($somedata){ // do something ... } 

Comments

0

You myMethod() method is public, so therefore can be accessed anywhere. Make sure you include all necessary files like so:

require_once('myPlugin.php') require_once('anotherClass.php') 

Then simply write something like this:

// 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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.