Page 1 of 1

How to create variable names dynamically

Posted: Fri Jul 21, 2006 10:23 pm
by Benjamin
If this is possible I would like to know how to do it..

Code: Select all

class sample
{
    function __construct()
    {
        $this->transferPost();
    }

    function transferPost()
    {
        foreach ($_POST as $key => $value)
        {
            $this->$key = $value; // <-- 
        }
    }
}
So if a variable named thisName is posted, I can then access that variable with

Code: Select all

$howdy = $sample->thisName;
Maybe that code I wrote up there will work but I'm assuming it's not that easy..

Posted: Fri Jul 21, 2006 10:45 pm
by Christopher
Have you tried it? You could execute that very nice script faster than it takes to write a post.

Don't make me start talkin' like a pirate!

Posted: Fri Jul 21, 2006 10:52 pm
by Benjamin
:P Cool... Just in the middle of writing a big class and had a brainstorm.. Didn't realize it might work until after I finished writing it.

Posted: Fri Jul 21, 2006 11:02 pm
by Benjamin
You might want to change the channel... I think that station is off the air :D