0

I am working on a function set that will add a piece to each custom post type on a site. Since I won't know what CPTs are registered, I wrote a function to get them all (simple). However, I now need to create a function for each value in an array (a small settings page) to properly finish this off.

here's my array example:

$types = array ('type_a', 'type_b', 'type_c');

so I basically want to generate a function called type_a_page, type_b_page, etc within the same overall class.

UPDATE

I realized this code is only a small part and doesn't explain why I'm trying to achieve this. Here is the code base in it's entirety https://gist.github.com/4687698

3
  • 2
    This seems to be on PHP side? Could you elaborate how your needs refer to WP mechanics here? Commented Jan 31, 2013 at 23:01
  • Double @Rarst on this. Also: In what context to you need this? Hint: This will me verrrry slow. Commented Jan 31, 2013 at 23:28
  • 1
    Why do you need a new method for each post type? Use one method and decide inside what to do with the arguments. Commented Feb 1, 2013 at 6:35

2 Answers 2

0

Can you use the __call PHP class functionality?

http://www.php.net/manual/en/language.oop5.overloading.php#object.call

You could use __call in your class and call your function for grabbing the page types and check them against the $name (first) argument and running custom code against it.

For instance:

__call ($name, $args) {
    $types = array ('type_a', 'type_b', 'type_c');

    if (in_array($name, $types) {
        // custom code
    }
}

UPDATE: Response to gist.

So using __call as a method on your class, on line 90 of your gist, you would use:

$this->$slug();
5
  • 2
    Lone link is considered a poor answer (see FAQ) since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to. Commented Jan 31, 2013 at 22:59
  • I just added a code sample and specific summary. Commented Jan 31, 2013 at 23:06
  • 2
    @James Thompson thank you! you seem to have ended up with two accounts on site, would you like for me to merge them? Commented Jan 31, 2013 at 23:09
  • Sure - that'd be great :) Commented Feb 1, 2013 at 3:44
  • seems like my thought process was wrong, not the code method. marking this as the correct answer because it's the best based on my bad idea :) Commented Feb 5, 2013 at 15:29
0

Couldn't you use the PHP function create_function?

http://php.net/manual/en/function.create-function.php

1
  • 2
    Link only answers are considered poor answers here. Could you provide sample code? Commented Jan 31, 2013 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.