![]() |
eval() issue
Hey all,
I'm new here and like to join the community to ask & answer! From the netherlands as a airline pilot student and part-time php programmer :) At my current project I got a situation in which I call a method from a child class and the parameter's are also defined in a config file. So using the config file info I build up the correct argument order, the variables are defined before that. Now I wanna use eval() to call my method but I get the following error: Quote:
PHP Code:
Think I need to escape it in some way but just putting a backward slash in front of $this won't work.. Thanks in advance! |
Already solved it now by changing the call into:
PHP Code:
|
Welcome to the forums, they are a little quite at the moment, but am sure they will start getting busy once again.
|
Thanks for the welcome :)
Thought I'd fixed it but nope.. it just seems the whole arg_order var as the first argument of the function.. quickfixed it this way but it's really ugly and there has to be a way to automate this: PHP Code:
|
I was going to suggest to accept in array form or multiple args, but you just said you don't like that. Welcome aboard!
|
|
That's a nice solution! Thanks guys :)
|
I would be cautious in calling eval() pure evil. Dynamic evaluation is a powerful tool and can sometimes be a life saver. With eval() one can work around shortcommings of PHP (see below).
The main problems with eval() are: • Potential unsafe input. Passing an untrusted parameter is a way to fail. It is often not a trivial task to make sure that a parameter (or part of it) is fully trusted. • Trickyness. Using eval() makes code clever, therefore more difficult to follow. To quote Brian Kernighan "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it" The main problem with actual use of eval() is only one: • Inexperienced developers who use it without enough consideration. As mentioned before eval() can help you do things that are impossible in pure PHP. My favourite trick involving dynamic evaluation enables static calls on variable classes. Since $foo::bar() is illegal in PHP, below solution works around that limitation. $className = 'Foo'; eval('$result = ' . $className . '::bar()'); echo $result; As a rule of thumb I tend to follow this: 1. Sometimes eval is the only/the right solution. 2. For most cases one should try something else. 3. If unsure, goto 2. 4. Else, be very, very careful. |
Quote:
I tend to find that those that use eval don't know the language well enough to do it any other way. There are very few cases to use eval. Your example can be done with the following code: PHP Code:
|
| All times are GMT. The time now is 05:59 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0