Wouldn't that be this?josh wrote:Code: Select all
foreach($this as $variableName => $variableValue ) { // now what ? }
Code: Select all
foreach ($this as &$variableValue) {
$variableValue = 'blah blah blah';
}Moderator: General Moderators
Wouldn't that be this?josh wrote:Code: Select all
foreach($this as $variableName => $variableValue ) { // now what ? }
Code: Select all
foreach ($this as &$variableValue) {
$variableValue = 'blah blah blah';
}Code: Select all
function foo() {
echo 'foo';
}
function bar() {
echo 'bar';
}
if(1==rand(1,2))
{
$command = 'bar';
}
else
{
$command = 'foo';
}
$command(); // sometimes says foo, sometimes says bar
Yes. In fact, the framework I'm writing depends on that feature for dynamically loading modules. Variable functions/classes are useful.josh wrote:Realize too, its not just variable variables - its variable function names, variable class names, in addition to variable variables.
Thank you, Captain Obvious.mtptl11 wrote:A variable is something that changes.
You can set protected/private variables using variable variables??? Can someone confirm that, as that may be interesting to know. It would serve as a last resort hack if required in some situations.That's assuming you wanted to set every public variable of the class, maybe it was protected, or maybe you didn't want to set every one. Point is, they [variable variables] have a use....
This conversation was strictly about variable variables. I had simple never found a good use for them, other than a more obscure way of setting variables or (my understanding at the time of writing) an interesting way to dynamically retreive the name of the variable - which while neat seemed utterly pointless.Realize too, its not just variable variables - its variable function names, variable class names, in addition to variable variables.
That is very common, variable function names make the code more elegant, as opposed to using call_user_x() functions, IMO. But variable variables do not look pretty, in fact they look downright encryptic, almost like Pearl code. However if you can indeed set private and protected variables using them...there have been a few times I have been tempted to seek a solution such as this, but I always favored refactoring or extending the class instead, probably for the best.Yes. In fact, the framework I'm writing depends on that feature for dynamically loading modules. Variable functions/classes are useful
You can't do that using variable variables as far as I know (you could read privates by casting the object to array in some older php versions). And there's standard and documented way to access privates via reflection: http://us2.php.net/manual/en/reflection ... ssible.phpPCSpectra wrote:You can set protected/private variables using variable variables??? Can someone confirm that, as that may be interesting to know. It would serve as a last resort hack if required in some situations.That's assuming you wanted to set every public variable of the class, maybe it was protected, or maybe you didn't want to set every one. Point is, they [variable variables] have a use....
Code: Select all
class ReadOnlyWrapper
{
protected $object = null;
public function __construct($object)
{
if (!$object)
throw new Exception('Object instance required.');
$this->object = $object;
}
public function __set($property, $value)
{
throw new ReadOnlyException('Object oproperties have read only access');
}
public function __get($property)
{
if (is_object($this->object->{$property}))
return new ReadOnlyWrapper($this->object->{$property});
else
return $this->object->{$property};
}
public function __call($method, $arguments)
{
if (substr($method, 0, 3) === 'get')
call_user_func_array(array($this->object, $method), $arguments);
else
throw new ReadOnlyException('Method ['. get_class($this->object).'->'.$method.'] called is not a read only one.');
}
}
class ReadOnlyException extends Exception {}Code: Select all
$user = $this->findUser(4);
$this->load->view('user/save/form', new ReadOnlyWrapper($user));Code: Select all
<?php
class You_Are_Lazy
{
protected $test;
function test()
{
$var = 'test';
$this->$var = 'you are lazy!';
echo $this->$var;
}
}
$test = new You_Are_Lazy();
$test->test();
A slight miscommunication. I think they meant something more like:josh wrote:Too lazy to try it out? Of course you can:
I don't see how this comes as a surprise, really - or why it would suddenly change your views about the feature.Code: Select all
<?php class You_Are_Lazy { protected $test; function test() { $var = 'test'; $this->$var = 'you are lazy!'; echo $this->$var; } } $test = new You_Are_Lazy(); $test->test();
Code: Select all
<?php
class You_Are_Lazy
{
protected $test = 'you are lazy!';
}
$test = new You_Are_Lazy();
$var = 'test';
echo $test->$var;
Code: Select all
$things = mysql_fetch_assoc($result);
foreach($things as $key => $value) {
$$key = $value;
}
Reminds me of register globals. Why do we hate arrays so much!?JoeCommodore wrote:Here is my example:
This is the type of stuff I use it for the most - reading in db fields into variables:Same goes for initializing and reading in filtered POST data into variables (from a an array of POST variable names I specify)Code: Select all
$things = mysql_fetch_assoc($result); foreach($things as $key => $value) { $$key = $value; }
My comment was directed only to the person I quoted, not the entire thread. However, it reminds me of register globals because there is no clear way to determine exactly where a particular variable came from. Especially if we are using user input to inject variables into our script, exactly as register globals would do, there is the potential for unintended behavior to occur. Similiar to the other example JoeCommodore gave (turning data columns into variables), what happens when we introduce new columns in the future ... again there is potential for unintended behavior. That doesn't go to say something bad will happen, you can of course implement them in a safe manner, but that doesn't mean it's right. We could have used register globals in a safe manner, but the potential for harmful behavior outweighs the utility of it.I don't understand what variable variables and register global have in common?
Of course it is, but that is what parameters are for.If you had to use a few variable a lot within a method for really complex logic, its more readable to have the variable instead of the array. And why have multiple lines of variable assignments when all you had to type was one additional '$'
Code: Select all
doSomething($request['foo'], $request['bar']);
function doSomething($somethingMeaningful, $importantToKnowExactlyWhatThisIs)
{
//blah
}