3

Is it possible to create a variable variable pointing to an array or to nested objects? The php docs specifically say you cannot point to SuperGlobals but its unclear (to me at least) if this applies to arrays in general.

Here is my try at the array var var.

     // Array Example
     $arrayTest = array('value0', 'value1');
     ${arrayVarTest} = 'arrayTest[1]';
     // This returns the correct 'value1'
     echo $arrayTest[1];
     // This returns null
     echo ${$arrayVarTest};   

Here is some simple code to show what I mean by object var var.

     ${OBJVarVar} = 'classObj->obj'; 
     // This should return the values of $classObj->obj but it will return null  
     var_dump(${$OBJVarVar});    

Am I missing something obvious here?

2
  • Updated my answer to include the array index problem too, I misread you and thought you had that under control! Commented Jan 10, 2010 at 9:56
  • If variable-variable is the answer, you are probably asking wrong question. Why not just use by-reference assignment? Commented Jan 17, 2010 at 11:34

5 Answers 5

5

Array element approach:

  • Extract array name from the string and store it in $arrayName.
  • Extract array index from the string and store it in $arrayIndex.
  • Parse them correctly instead of as a whole.

The code:

$arrayTest  = array('value0', 'value1');
$variableArrayElement = 'arrayTest[1]';
$arrayName  = substr($variableArrayElement,0,strpos($variableArrayElement,'['));
$arrayIndex = preg_replace('/[^\d\s]/', '',$variableArrayElement);

// This returns the correct 'value1'
echo ${$arrayName}[$arrayIndex];

Object properties approach:

  • Explode the string containing the class and property you want to access by its delimiter (->).
  • Assign those two variables to $class and $property.
  • Parse them separately instead of as a whole on var_dump()

The code:

$variableObjectProperty = "classObj->obj";
list($class,$property)  = explode("->",$variableObjectProperty);

// This now return the values of $classObj->obj
var_dump(${$class}->{$property});    

It works!

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

1 Comment

Thanks! Exactly what I was trying to accomplish. Worked Perfectly.
3

Use = & to assign by reference:

 $arrayTest = array('value0', 'value1');
 $arrayVarTest = &$arrayTest[1];

 $arrayTest[1] = 'newvalue1'; // to test if it's really passed by reference

 print $arrayVarTest;

2 Comments

asker is not asking about referencing.
@mauris : perhaps but finding this answer while googling gave me solution I was looking for ;)
0

In echo $arrayTest[1]; the vars name is $arrayTest with an array index of 1, and not $arrayTest[1]. The brackets are PHP "keywords". Same with the method notation and the -> operator. So you'll need to split up.

// bla[1]
$arr = 'bla';
$idx = 1;
echo $arr[$idx];

// foo->bar
$obj = 'foo';
$method = 'bar';
echo $obj->$method;

What you want to do sounds more like evaluating PHP code (eval()). But remember: eval is evil. ;-)

Comments

0

Nope you can't do that. You can only do that with variable, object and function names.

Example:

 $objvar = 'classObj';
 var_dump(${$OBJVarVar}->var);

Alternatives can be via eval() or by doing pre-processing.

$arrayTest = array('value0', 'value1');
$arrayVarTest = 'arrayTest[1]';

echo eval('return $'.$arrayVarTest.';');
eval('echo $'.$arrayVarTest.';');

That is if you're very sure of what's going to be the input.

By pre-processing:

function varvar($str){
  if(strpos($str,'->') !== false){
    $parts = explode('->',$str);
    global ${$parts[0]};
    return $parts[0]->$parts[1];
  }elseif(strpos($str,'[') !== false && strpos($str,']') !== false){
    $parts = explode('[',$str);
    global ${$parts[0]};
    $parts[1] = substr($parts[1],0,strlen($parts[1])-1);
    return ${$parts[0]}[$parts[1]];
  }else{
    return false;
  }
}

$arrayTest = array('value0', 'value1');
$test = 'arrayTest[1]';
echo varvar($test);

Comments

0

there is a dynamic approach for to many nested levels:

$attrs = ['level1', 'levelt', 'level3',...];
$finalAttr = $myObject;
foreach ($attrs as $attr) {
    $finalAttr = $finalAttr->$attr;
}
return $finalAttr;

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.