Probably because there was an unexpected $ in the eval function. :lol:
I have no idea, I’m just an interested bystander, and enjoying this thread.
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
July 10, 2008 6:18am
Subscribe [0]#31 / Jul 11, 2008 8:06am
Probably because there was an unexpected $ in the eval function. :lol:
I have no idea, I’m just an interested bystander, and enjoying this thread.
#32 / Jul 11, 2008 8:16am
I got unexpected $end, which means that a “;” is missing at the end.
This should work:
function address($string)
{
eval('$'.trim($string,'$;').';');
}#33 / Jul 11, 2008 9:03am
adding the semicolon did the trick and the average is 6 times slower if you use it inside the function and 10 times slower if you use it outside the function. The thing that slows down the outside solution is probably the second if in the default loop to add the variables to the return array.
function process_args2($added_args,$default_args)
{
if(count($added_args) == count($default_args))
{
return array_combine(array_keys($default_args),$added_args);
}
else
{
foreach($added_args as $arg)
{
eval($arg.';');
}
foreach($default_args as $key=>$def_val)
{
if(isset(${$key}))
{
$added_args[$key] = ${$key};
}
if( ! is_null($def_val) && ! isset(${$key}))
{
$added_args[$key] = $def_val;
}
}
return $added_args;
}
}#34 / Jul 11, 2008 11:57am
Randy i think the problem with parameters without a default value is that they produce an error.
Not in either of my solutions! My solution will take any number of parameters, including zero, in any order, with or without a named variable, and no predefined parameters at all, and certainly not with default values.
There is that one little kludge though…
Let me know when you’re want to see it.
#35 / Jul 11, 2008 11:58am
Wanna wanna wanna seeeeeeeeeee 😉
Oh yes have you benchmarked it?
#36 / Jul 11, 2008 12:33pm
No need I feeeeeel the speed.
All the delays were just to make sure you were psychologically stable enough, and mentally able to deal with me winning twice in a row ! :sick:
OK—Prepare to bring out the hoses…
Now, frankly, I don’t even know what the challenge is. You said:
So the challenge is how do you hack user defined function arguments.
and I don’t have a clue what in the world that means. But you also said this…
I’ve searched to find a way to do;
address($name = 'me'); address($street = 'fifth avenue');Which isn’t possible.
And the fact is, it is possible, and that is the challenge I’ve succeeded at. If that means I’ve succeeded at whatever “hack user defined function arguments” means, great. All I know, is that you can do:
address($name = ‘me’);
address($street = ‘fifth avenue’);
And here’s the code to do it…
function doIt(){
$count = 0;
foreach(func_get_args() as $var){
$input = $var;
$varName = 'var'.$count;
global $$varName;
echo 'Name: '.$$varName."
";
echo "Value: $input
";
$count++;
}
}Push this (or as my previous posts have indicated ANY arguments to it)...
doIt($name = 'me');
doIt($street = 'fifth avenue');Simplicity is a beautiful thing.
This will output nothing for the Name: value and the Value: of the variable. To get the Name: to output the kludge is needed. This code does not require modification in any way. The explanation of the kludge comes in the next post.
Randy
#37 / Jul 11, 2008 12:43pm
Alrighty then…
The kludge revolves around the severe scoping problem that creeps into the variable variable use OUTSIDE the function. The only way to gain access to the NAME of the variable variable is the declare it Global INSIDE the function. That’s not even the kludgy part yet. If you think about this for a second, you might wonder “ok, so what do you call it then?” Immediately you discover that you take away all of the flexibility away from the function user.
Enter syntax constraints and language conventions…
In order to use the function to link a variable name with the associated scalar value, you must declare the variable name in the following manner:
$var0 = ‘name’;
$var1 = ‘address’;
Each argument must be assigned to a variable named $var# starting from $var0 thru $varN. The variable names can be assigned in any order as long as the $var sequence is adhered to.
So from the previous post add:
$var0 = 'name';
$var1 = 'address';
doIt($name = 'me');
doIt($street = 'fifth avenue');Now the doIt() function will echo both the Name: and Value: of the variables.
Ok—proceed to trash it. (but but but—but it’s beautiful and you know it!)
Randy
#38 / Jul 11, 2008 2:11pm
I’ve made two typos….
This is the correct sequence:
$var0 = 'name';
doIt($name = 'me');
$var0 = 'street'; // <-- Changed $var# to zero, change order and changed string to street
doIt($street = 'fifth avenue');To do it the other way would work find but the function call would be different:
$var0 = 'name';
$var1 = 'address';
doIt($name = 'me', $street = 'fifth avenue'); // <-New function callNow the $var#‘s match the order of arguments in the function call.
Here is a whole list…
$var0 = 'name';
$var1 = 'street';
doIt($name = 'sam', $street = 'fifth avenue');
echo '<hr>';
doIt($name = 'sam');
echo '<hr>';
$var0 = 'street';
doIt($street = 'fifth avenue');
echo '<hr>';
$var0 = 'name';
$var1 = 'street';
$name = 'Sam';
$street = 'fifth avenue';
doIt($name, $street);
echo '<hr>';
doIt('sam');
echo '<hr>';
$var0 = 'street';
doIt('fifth avenue');
Sorry ... I was just so excited 😉
#39 / Jul 11, 2008 2:39pm
I’ll borrow wiredesignz’s verbiage here:
Randy, what the hell is that.
1. Globals - YUCK!
2. No default values
3. Scoping headache if I’ve ever seen one.
#40 / Jul 11, 2008 2:59pm
I told you it was a flippin’ kludge! :red:
But you have to admit it is one hell of a lot closer to the initial challenge than THIS.
It was about function calls for Pete’s sake! hehehe
And when someone says “this can’t be done” it doesn’t really matter about “globals YUCK”, and headaches and all. It’s the “it can’t be done” part that is now behind us forever—and that is undeniable! BAM! as the famous Chef says. 😛
Randy
#41 / Jul 11, 2008 3:39pm
@inparo—
Ok—here is in a class for you an wiredesignz:
class myClass {
var $data;
function doIt(){
foreach($this->data as $k=>$v){
echo "Name: $k
";
echo "Value: $v
";
}
}
function setVar($var,$val){
$this->data[$var] = $val;
}
}Feed it this:
$coolness = new myClass();
$coolness->setVar('name','sam');
$coolness->setVar('street','fifth avenue');
$coolness->doIt();I just thought anyone could do that. Besides, it was about functions. And xwero asked about performance. So this one definitely needs a benchmark—but that’s up to you guys becuase the other one blows this one (and all the others) completely out of the water.
So I guess “pick you battles” comes to mind. There are several solutions to pick from.
Randy :coolsmile:
#42 / Jul 11, 2008 5:04pm
function run($page = 'default',$page2 = 'default2') {
$params = func_get_args();
foreach($params as $param) {
eval($param.';');
}
echo $page, $page2;
}
run('$page = "My Page"');Returns
My Pagedefault2Not quite what I think your looking for but tell me what needs to be changed in the arguments that are supplied and I see if I can do something about it.
#43 / Jul 11, 2008 10:52pm
While this exercise may have some use, any answer to the problem is worthless. Unless you are attempting to write an inefficient piece of code, there is no use case to implement a complex behavior when PHP supports a simple solution. The first reply to this post is the best method to achieve a behavior (a behavior that is unsupported by PHP as noted by the OP) close to what the OP is looking for. That answer:
‘You pass them as an array.’
#44 / Jul 12, 2008 12:26am
Party pooper 😛 —Just kidding 😉 You sound a lot like me most of the time. In fact, xwero it’s as if my attitude from the last challenge was just morphed….hehe.
Seriously, though there was just a great discussion about “Functional Programming” (link in previous post) with references provided, etc. to broader thoughts on expanding conceptual thought beyond the static day-to-day do-it-the-way-your-fingers-naturally-fall-on-the-keyboard thingy. I’m not sure what xwero’s motivation was, but I thought it would be fun to stretch my imagination and “try and out wit” these folks that have all proven they’re pretty damn smart about this stuff.
Now, with that said…I have seen a very particular use case come up over and over an over again for this specific “type” of function argument hacking. It relates specifically to the SQL SELECT statement and all of its various incarnations. The darn thing is so complicated that it is really difficult to “abstract” in a way that makes function calls graceful, easy to implement without a whole slew of round robins. We classically end up with multiple functions to build a single LEFT JOIN containing both a LIMIT and a SORT BY clause.
Forget SQL for a minute. Don’t forget about the popularity of JS and all of its spin offs. This idea resides at its core. So there certainly is a use case out there somewhere for this concept.
As you state though, the benefit to a well oiled PHP/SQL coding machine is negligable since these matters are of no consequence.
Sometimes in forums:
‘You pass them as an array.’
becomes “how do I do that? But wouldn’t it be cool to be so abstract for a new user to simply say “send the table, fields, the field you want sorted, and the number of rows you want returned” in this format $var=‘val’ and have them make the call:
getData($table='myTable',$fname ='firstName',$lname='lastName',$order='firstname',$lim='10');
//or
getData($fname ='firstName',$lname='lastName',$table='myTable',$order=$fname,$lim='10');
//or
getData($table='myTable',$lname='lastName',$lim='10');
//or
getData($table='myTable',$fname ='firstName',$order=$lname,);without a care in the world about the order of things, etc. Of course there would have to be rules, but you get my drift. If the arguments could come out of order, or any order there are hundreds of use cases for functions like that.
And PHP is moving in that direction with every revision. Namespaces are being added officially to 6.0 and will be released with v5.3. These are the types of features that will lead us down the road to broader minds.
—————-
[EDIT]Was this excerise Worthless? NO. [/EDIT ] Was this exercise very useful? Probably not. Fun? Definitely! Worth the time and effort? Yup. Learn anything? I sure did!
Thanks for the challenges xwero!
Randy
#45 / Jul 12, 2008 1:35am
But you have to admit it is one hell of a lot closer to the initial challenge than THIS.
It’s closer to the first post, yes. However, xwero made a few clarifications to usage, which make my solution the better one to implement.
It doesn’t really matter though. I didn’t write it to implement it, or even to find a solution. It’s the creative process that comes with these challenges that I love, and see a lot of benefit in.
We get so used to doing things one way, it’s very easy to lose sight of the creative spark that makes programming so much fun.