[resolved] eval(), arrays and type_cast errors...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mko_san
Forum Newbie
Posts: 3
Joined: Thu Jul 27, 2006 5:52 pm

[resolved] eval(), arrays and type_cast errors...

Post by mko_san »

Hello, I stumbled upon an error I can't seem to iron out myslef, and decided to try and seek some help ^__^

Code: Select all

function parse_me_on_the_fly () {
	global $skin;

	//how many damn ages do we have?
	$args_num = func_num_args();	
	$arg_list = func_get_args();
	
	//name and set...
	$group_name = $arg_list[0];
	$name = $arg_list[1];	
	
	//get argnames	
	$args = $skin[$group_name][$name]['args'];

	//remove spaces
	if ($args) {
		$args_l = strtr($args, ' ', '');
		$args_l = explode(',', $args_l);
	}
	
	//rest...
	$sweet_args = array();
	if ($args_l) {
		$count = count($args_l);
		for ($i = 0; $i < $count; $i++) {
			$sweet_args[] = '$arg_list['.($i+2).']';
		}
		$sweet_args = implode(',', $sweet_args);
	}

	// can we get it done yet?
	$func = create_function ($args, $skin[$group_name][$name]['content']);

	//got it? then do it!
	return eval("return \$func($sweet_args);");
} //parse_me_on_the_fly
produces bunch of:

Code: Select all

Parse error: parse error, unexpected T_ARRAY_CAST in /srv/www/htdocs/web172/html/sub/core/func/template.php(88) : eval()'d code on line 1
errors [when non-array vars are passed to the eval'd function].

Here's a short description of what above function does, just to cleat things up a bit... What this function is doing is:

- accepting x arguments. First two are mandatory, rest is optional. They are captured to $arg_list.
- the first two arguments are needed to locate data within $skin array. Using that info we are able to import a string of arguments $args and a body of a function as another string
- those two imported bits of data are then used to create a function stored in $func

- next we want to catch all the additional arguments coming into the main function. To use them as arguments of the function we just created we have to convert the additional arguments in $arg_list to a string of argument names, making ie "arg_list[2], arg_list[3]"

- then we use eval to call created function with the additional arguments.

All this is because the created function arguments as well as body is made somewhere else, and we want to parse them into a function and run with the arguments that are passed into the parent function [parse_me_on_the_fly()]. We don't know how many arguments there will be, hence the need for fancy stuff.

The qestion obviously is why am I getting those type cast errors, and how to fix that ^_^
Last edited by mko_san on Fri Jul 28, 2006 5:49 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

// can we get it done yet?
        echo "function($args) {\n {$skin[$group_name][$name]['content']}\n}";
//      $func = create_function ($args, $skin[$group_name][$name]['content']);

        //got it? then do it!
        echo "return \$func($sweet_args);";
//      return eval("return \$func($sweet_args);");
what does above yield?
mko_san
Forum Newbie
Posts: 3
Joined: Thu Jul 27, 2006 5:52 pm

Post by mko_san »

The problem with checking it this way is the fact that $skin[$group_name][$name]['content'] contains raw html wrapped in here doc. So you can only imagine the mess those echoes make. However, I'll try to put some of the output here.

the exampl of the first echo you requested:

Code: Select all

function($link) { global $INFO; $html = ""; //--starthtml--// $html .= <<UNESCAPED; //--endhtml--// return $html;
above corresponds to $skin[$group_name][$name]['content'] that contains

Code: Select all

$html = ""; 
$html .= 
<<<UNESCAPED; 
<tr><td class="menu">{$link}</td></tr>
UNESCAPED;
return $html;
and $skin[$group_name][$name]['args'] = "$link"

above echo with eval() added produces:

Code: Select all

return $func($arg_list[2]);function($link) { global $INFO; $html = ""; //--starthtml--// $html .= <<UNESCAPED; //--endhtml--// return $html; }
However! there was something interesting in that mess of the output. It seems that the crated function without any arguments is somehow expected to have an array as an argument by eval. Look at this:

Code: Select all

function() { global $INFO; $html = ""; //--starthtml--// $html .= <<UNESCAPED; //--endhtml--// return $html; }return $func(Array);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

$sweet_args is an array, so if you try to use it like a string you just get "Array". Implode it with commas.
mko_san
Forum Newbie
Posts: 3
Joined: Thu Jul 27, 2006 5:52 pm

Post by mko_san »

d11wtq wrote:$sweet_args is an array, so if you try to use it like a string you just get "Array". Implode it with commas.
Um... but it is imploded with comas... look at the code in my first post.

EDIT:

Actually... you are a genius! The implode() was skipped when no arguments were passed to created function. Once I put it outside that if statement it all started to work. I knew there had to be some stupid mistake that I missed xD

Thanks ^___^
Post Reply