Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I used to develop in functional design so far and I am now learning OOP. I stumbled upon following problem.

Sometimes in OOP a function needs to send and argument as an array.

Example. I want to add an admin page for a plugin in Wordpress.

In functional design the function looks like this:

function myclass_plugin_admin_add_page() {
    add_options_page('MyClassPlugin Page', 'MyClassPlugin Menu', 'manage_options', 'do_myclass', 'myclass_plugin_options_page');
}

In OOP the last argument has to look like this:

function myclass_plugin_admin_add_page() {
    add_options_page('MyClassPlugin Page', 'MyClassPlugin Menu', 'manage_options', 'do_myclass', array($this, 'myclass_plugin_options_page'));
}

How the heck will I ever know which argument I have to change to an array when writing the same function in OOP???

share|improve this question
add comment

1 Answer

Replace this line:

add_options_page('MyClassPlugin Page', 'MyClassPlugin Menu', 'manage_options', 'do_myclass', 'myclass_plugin_options_page');

with

add_options_page('MyClassPlugin Page', 'MyClassPlugin Menu', 'manage_options', 'do_myclass', array( $this, 'myclass_plugin_options_page' ) );

i.e. you need to call the class method with

array( $this, 'myclass_plugin_options_page' )

instead of

'myclass_plugin_options_page'
share|improve this answer
    
Thanks birgire! I just found out your answer myself and rephrased my question. How can I find out which function's arguments I have to pass as an array when switching to OOP? –  AleV Jul 24 '13 at 12:13
1  
ok I see you changed the question, maybe you should add the new part after the original question to avoid confusion ;-) Usually it's done to the class methods. Check out the tutorials on hardcorewp.com (for example), they are really helpful, like this one: hardcorewp.com/2012/… –  birgire Jul 24 '13 at 12:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.