Some code uses eval()
to execute code stored in an array like this:
// $oOwner is an object
$strId = "abc";
$strClass = "someClass";
$aParams = array('a' => 'atext', 'b' => 'btext');
$this->menu = array(
"Entry 1" => ' openForm(\$oOwner,\$strId,\$strClass,\$aParams); ',
// ...
);
The values of the array's keys will be given directly into an eval()
function.
Now I get an error: Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING ... eval()'d code on line 1
What is the problem?
EDIT:
Loop through the array and eval()
the values:
eval( $this->menu[$param[0]] );
EDIT 2:
Now: "Entry 1" => " openForm(\$this->owner,\$strId,\$strClass,\$aParams); "
Using double quotes "..."
leads to PHP Notice: Undefined variable: strId in ... : eval()'d code on line 1
. Also for the other variables.
`
The way I tried always led to errors and I came up with the idea of putting the array in there as a string via var_export()
.
There are 2 attempts posted here: Store array in a string to eval() later
You are using single quote '
so you don't need to use \$
in your variables
Prof of Concept use both single and double quotes
function openForm(){
var_dump(func_get_args());
}
$strId = "abc";
$strClass = "someClass";
$aParams = array('a' => 'atext', 'b' => 'btext');
$oOwner = "ABC" ;
$menu = array(
"Entry 1" => ' openForm($oOwner,$strId,$strClass,$aParams); ',
"Entry 2" => " openForm(\$aParams,\$strId); "
);
eval($menu["Entry 1"]);
Output
array
0 => string 'ABC' (length=3)
1 => string 'abc' (length=3)
2 => string 'someClass' (length=9)
3 =>
array
'a' => string 'atext' (length=5)
'b' => string 'btext' (length=5)
array
0 =>
array
'a' => string 'atext' (length=5)
'b' => string 'btext' (length=5)
1 => string 'abc' (length=3)
Have you tried double quotes when declaring code to be eval?
// ...
$this->menu = array(
"Entry 1" => " openForm(\$oOwner,\$strId,\$strClass,\$aParams); ",
// ...
);