Comments on: How to use variable variables in PHP https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/ building stuff on the web with Josh Fraser Wed, 13 Dec 2017 07:06:00 +0000 hourly 1 By: Jatinder Verma https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-74691 Wed, 13 Dec 2017 07:06:00 +0000 https://onlineaspect.com/?p=255#comment-74691 But the problem is that field variable is now local variable.. 🙁

]]>
By: bayard barnslee https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-5039 Thu, 21 Feb 2013 19:44:08 +0000 https://onlineaspect.com/?p=255#comment-5039 The presence of variable variables in code is a flashing red light that the programmer was either 1) a hack and a lazy one at that, or 2) had to work with a mess left by a lazy hack and had no time to refactor it so variable variables are not needed.

In both cases the hack is using variable variables like a Looney Tunes cartoon character uses an Acme Portable Hole — whenever they realize they're backed into a corner, they resolve the dilemma by dropping a Portable Hole and bailing or having the bad guy disappear in it. Problem solved.

Variable variables in the code is almost always the sign of a clever amateur who is in way over his head but is too full of himself to realize that.

]]>
By: dmjones https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-4584 Tue, 20 Dec 2011 16:45:57 +0000 https://onlineaspect.com/?p=255#comment-4584 The alternative was suggested and works well

$this->{'_' . $key} = $val;

]]>
By: dmjones https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-4582 Tue, 20 Dec 2011 06:35:30 +0000 https://onlineaspect.com/?p=255#comment-4582 I wanted to use this avenue in order to populate private variables of a class using an array as input. However, the values don't seem to stay or aren't even assigned to the private vars. Any ideas?

class aClass {
private $_fruitA;
private $_fruitB;
private $_fruitC;

public function set($inputArray)
{
foreach ($inputArray as $key=>$val)
{
$var = "this->_{$key}";
$$var = $val;

//prints nothing
print "this->_$key: ".$this->_fruit."n";

//prints the $val assigned in $inputArray
print "$$var: ".$$var."nn";
}
}
}

$fruit = new aClass();
$fruit->set(array('fruitA' => 'apple', 'fruitB' => 'banana'));

]]>
By: Scat DVD https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-3254 Sun, 30 Oct 2011 17:20:05 +0000 https://onlineaspect.com/?p=255#comment-3254 Variables are used for storing values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. thanks for sharing the informative post.

]]>
By: Josh Fraser https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-755 Wed, 18 Nov 2009 18:41:59 +0000 https://onlineaspect.com/?p=255#comment-755 In reply to robert.

Try this http://bit.ly/1eiBtw

]]>
By: Josh Fraser https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-756 Wed, 18 Nov 2009 18:41:59 +0000 https://onlineaspect.com/?p=255#comment-756 In reply to robert.

Try this http://bit.ly/1eiBtw

]]>
By: robert https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-754 Wed, 18 Nov 2009 16:08:03 +0000 https://onlineaspect.com/?p=255#comment-754 I came to your post looking for an aswer for my PHP class assigment. Proffesor asked, 3.When is better to use a local variable? I can't seem to find an aswer for any. Any help will be great.

]]>
By: Josh Fraser https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-686 Thu, 03 Sep 2009 22:49:10 +0000 https://onlineaspect.com/?p=255#comment-686 In reply to fractalbit.

Yeah, the formatting is a bit wonky. Sorry about that. I use a service called IntenseDebate for my comments. They give me a bunch of awesome features but it means I have to deal with a couple small issues like formatting and the inability to edit a comment. Totally worth the trade-off if you ask me.

]]>
By: fractalbit https://onlineaspect.com/2009/05/31/how-to-use-variable-variables-in-php/comment-page-1/#comment-679 Wed, 02 Sep 2009 18:14:21 +0000 https://onlineaspect.com/?p=255#comment-679 In reply to fractalbit.

hmmm, it doesn't appear very well, it removed tabs and br actually appears as a new line instead of printing it. Is there a special tag i should use when posting code? (ex. {pre}code{/pre} )

P.S. You added the ability to edit comments? 🙂 (Or is it because i am now registered to intenseDebate?)

]]>