Variables pointing to arrays or nested objects

Is it possible to create a variable variable that points to an array or to nested objects? The php docs indicate that you cannot point to SuperGlobals, but it is unclear (at least to me) if this applies to arrays in general.

Here is my attempt to var var array.

     // 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 a var var object.

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

Did I miss something obvious here?

+3
source share
4 answers

Array element :

  • Extract array name from string and save it to $arrayName.
  • Extract the index of the array from the string and save it to $arrayIndex.
  • Disassemble them correctly, not as a whole.

:

$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];

:

  • , , , (- > ).
  • $class $property.
  • , var_dump()

:

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

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

!

+5

= & :

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

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

 print $arrayVarTest;
+1

echo $arrayTest[1]; vars $arrayTest 1, $arrayTest[1]. - PHP. ->. .

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

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

, , PHP (eval()). : eval - .; -)

0

No, you cannot do this. You can only do this with the names of variables, objects, and functions.

Example:

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

Alternatives can be through eval () or by preprocessing.

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

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

That is, if you are very sure about what will be the entrance.

Preliminary 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);
0
source

Source: https://habr.com/ru/post/1727929/


All Articles