0

I want to declare a series of functions and name them using the elements of an array. I do not want to put the functions into an array as anonymous functions, but want them declared and accessible by calling them directly with their unique names from the elements of the array.

I want something like the following but that actually works:

for ($i=0;$i<count($tables);$i++) {

function $tables[$i][form]() {
}
// do something;
}

The purpose of these functions is to add submenu pages to my wordpress plugin's admin page. The number of submenus and their names depends on records in a table in the DB.

Perhaps there is a better way to do this altogether. Please help. When I tried using anonymous functions in an array and referencing the array elements in the function parameter in the add_submenu function, I kept getting permission problems when going to the submenu pages.

3
  • I am trying to ask the question in a clearer way. I wasn't sure if I should just change the original question to try to make it clearer or ask another one. If I changed the original question to say I don't want to use anonymous functions, then the responses suggesting I use anonymous functions would seem silly, and I didn't want that as I really do appreciate any help. Commented Mar 3, 2014 at 15:38
  • Where does "do something" come from? The DB? Commented Mar 3, 2014 at 15:41
  • I just put it there in place of all the stuff that the function does. Namely, it creates a tabbed menu with 5 pages to create new, list, view, edit and delete records in tables in the database. I'm trying to provide users with the ability to create their own tables and for this code to then provide them with an interface to create, view, list, edit and delete records in their tables. Commented Mar 3, 2014 at 15:49

2 Answers 2

0

The name of your function has to be a constant. But you can still call a function whose name is stored inside an array.

How to call a function with a dynamic name.

http://www.php.net/call_user_func_array

You can also use anonymous function, but this is a little bit more difficult.

http://www.php.net/manual/en/functions.anonymous.php

Sign up to request clarification or add additional context in comments.

3 Comments

I think my problem is I am having trouble doing what you suggest in the wordpress add_submenu framework. <?php add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function ); ?> is where the function gets called from.
Im no wordpress dev, but you might want to check the API on what type the $function parameter is. If it's a callable, you could either use an anonymous function or a string containing the name of your function.
It is described as a callback. I have tried both of these ideas and run into trouble. When I use the anonymous function, I get You do not have sufficient permissions to access this page. and when I use the string I am told that the function does not exist or has an invalid name.
0

The eval function was the solution I was looking for:

for ($i=0;$i<count($tables);$i++) {
  $a[] = $tables[$i][form];
  }    

// Loop through the names of the submenu pages creating the required functions

foreach ($a as $functionname)

eval('function '.$functionname.' () {
// All the function code goes here
}');

I found that not escaping in and out of php appropriately was causing most of my problems especially when trying to reference variables.

Thanks Barmar for pointing me to PHP: define functions with variable names

Comments

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.