How can I do this without using eval() ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jakeunna
Forum Newbie
Posts: 4
Joined: Wed Dec 22, 2010 6:12 pm

How can I do this without using eval() ??

Post by jakeunna »

Hi, I have one function which can accept an arbitrary number of variables. I want to pass all of these variables to another function. I have written the following script to do this.

Code: Select all

    public function addMeta()
    {
        $args = '';
        $numargs = func_num_args();
        $params = func_get_args();
        $i = 0;
        foreach($params as $value)
        {
            $i++;
            $args .= '\''.$value.'\'';
            if($i != $numargs)
            {
                $args .= ', ';
            }
        }
        return eval('$this->addClosedTag($args);');
    }
Does anyone have a way of doing this without using the eval() construct? This method will get called frequently, and it would be far more efficient to not use eval(). Also, I have heard the quotation "if eval is the answer then you are probably asking the wrong question", so I feel like I should be doing all I can to avoid using it!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How can I do this without using eval() ??

Post by Jonah Bron »

jakeunna wrote:"if eval is the answer then you are probably asking the wrong question", so I feel like I should be doing all I can to avoid using it!
That's right. Change addClosedTag() to accept an array of arguments.
jakeunna
Forum Newbie
Posts: 4
Joined: Wed Dec 22, 2010 6:12 pm

Re: How can I do this without using eval() ??

Post by jakeunna »

Jonah Bron wrote:Change addClosedTag() to accept an array of arguments.
Yeah, it seems like I'm going to have to do that. I was ideally after a solution which didn't require addClosedTag() to be changed, but I have pretty much concluded that it is impossible to do this with PHP. If anyone can do so, I'd love to be proved wrong!
jakeunna
Forum Newbie
Posts: 4
Joined: Wed Dec 22, 2010 6:12 pm

Re: How can I do this without using eval() ??

Post by jakeunna »

Aha! I have just stumbled upon the function call_user_func_array() - I can easily achieve what I want to achieve with this function. Problem solved.
jakeunna
Forum Newbie
Posts: 4
Joined: Wed Dec 22, 2010 6:12 pm

Re: How can I do this without using eval() ??

Post by jakeunna »

This is the modified function; it works perfectly without the need for eval.

Code: Select all

    public function addMeta()
    {
        $args = func_get_args();
        return call_user_func_array(array($this, 'addClosedTag'), $args);
    }
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How can I do this without using eval() ??

Post by Jonah Bron »

That works, but it's messy. The intent is not as clear as it can be. Changing addClosedTag is very simple:

Code: Select all

// whereas before it was like this:
function addClosedTag() {
    $args = func_get_args();
    // ...
}

// now, it's this:
function addClosedTag($array = null) {
    $args = func_get_args();
    if (is_array($array) && count($args) == 1) {
        $args = $array;
    }
    // ...
}
See how that works?
Post Reply