0

I have a PHP function that I need to create that executes PHP or HTML Code that is stored in a variable. That's one problem. Another problem is the fact that the function will be created multiple times and will need a unique name so I've named it after $title but I'm not sure if this will create the function from the value of $title or if it will create a function called $title or if it just won't work. This is my code:

$title = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];

Some Code Here...

function $title ($output)
{
    //Some Code Here to execute $output...
}

So... That's my problem. I'm not sure what will execute the $output cleanly so it can be shown on a page. And I don't know if the function will be named after the value of $title

Thanks for any help!

Ethan Brouwer

3
  • 4
    Why do you need a unique function with a new name each time? Pass these parameters to a single function and process the data accordingly. Commented Jul 7, 2012 at 12:12
  • are you talking about something like the eval() function? Commented Jul 7, 2012 at 12:15
  • 1
    Don't use eval(). It is a dangerous function and its use is discouraged. Commented Jul 7, 2012 at 12:20

2 Answers 2

2

You can use call_user_func like this:

call_user_func($title, $output);

But it's really strange to change name of one function.

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

3 Comments

I know it's strange. I'm making a wordpress plugin that lets you add your own screen options to any page, but the code that I use needs a function for every option that will be executed when the option is clicked and also to save the values. Thanks though! This is what I need.
@EthanBrouwer IMO, this way is dangerous. You get $title from post, what will happen if input, submitted by user, contains ;? I'd suggest to think on different solution for your situation, but this will still work until you get better way. Good luck.
For the record $title($output); should also work in the most recent versions of PHP. But make sure you filter $title properly if you are collecting it from POST, this is hella dangerous otherwise.
0

What you're trying to do is unfortunately not possible. You could create namespaces if you need a common name to prefix your functions with.

namespace fArr {
    function func1 (args) {}
    function func2 (args) {}
}

To call them you can use:

namespace {
    fArr\func1($title, $output);
}

Generally you want to avoid creating totally anonymous functions. Maybe create a function that handles the title and output for all requests. Alternatively create a new object with the func name as a property and output assigned to it.

$title  = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];

// Store data in an object named after the value of $title
// Note that constant use of this can get very messy, and very confusing
$$title = (object) array('output' => $output);

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.