Page 2 of 3
Posted: Wed May 09, 2007 2:10 pm
by McManCSU
Good point, but my experience is more versed in a C/C++ environment, so keeping things simpler than that base is preferred

Posted: Wed May 09, 2007 3:25 pm
by infolock
I don't want to sound argumentative here, but I just don't agree. However, each developer has his/her own way of coding.
To me, that's what the function declaration is there for, so that we can define a function to be reused later.
defining the function name to be a varable name is just redunant and unessary.
it's equalent to setting $i to 0, and then the very next line, saying $x = $i; then doing the rest of your opperations with $x and never using $i again.
But, like I said, to each their own. (=
Posted: Wed May 09, 2007 3:29 pm
by onion2k
infolock wrote:it's equalent to setting $i to 0, and then the very next line, saying $x = $i; then doing the rest of your opperations with $x and never using $i again.
Yeah, only it really isn't at all because you don't know where $func has been set. It might be coming from a database table having been set from an admin page for all you know.
Posted: Wed May 09, 2007 3:43 pm
by infolock
A function declaration??
Can you give me a better example, I'm not following you on that one.
Posted: Wed May 09, 2007 5:23 pm
by onion2k
$func is the function name, not the declaration.
A simple example would be a form builder. Imagine I give the user a system where they can select what validation function is applied to a form element. I could use a big switch case block, and remember to add a new case to it every time I define a new validation function.. or I could just store the function name in the database with the form element definition and dynamically call it..
Code: Select all
$sql = "SELECT * FROM `myform` WHERE `myform`.`form_id` = '1'";
$result = mysql_query($sql,$databaseLink);
while ($record = mysql_fetch_object($result))
{
$form_element_name = $record->form_element_name;
$validation_function = $record->validation_function;
$form_element_value = $_POST[$form_element_name];
if (function_exists($validation_function))
{
if (!$validation_function($form_element_value))
{
$errors[] = $form_element_name." failed to validate";
}
}
}
If you really wanted to give the user more control you could store the PHP code for the validation function in the database and pop it into create_function() at runtime. Once you get into writing code that features callbacks and dynamic loading you'll hit this sort of solution a lot.
Posted: Thu May 10, 2007 10:48 am
by infolock
But the problem you are missing here is that you already executed the function in that example.
What the guy is doing, from what i can tell, is something like this :
Code: Select all
function foo($var) {
// do something
}
$foo = 'foo';
$foo($some_var);
Which, is indeed, very redundant, and a poor use of resources.
Even the example you gave is not a valid argument. You already are executing the function (which is a function from a class), and then after executing it, you are checking if the function exists, and then re-running it again.
The point I'm making is, why take up all this memory from your server, just to do something that's actually a long winded solution?
It just seems very messy to do such a thing like this... even if it IS possible to to do it, it doesn't make it a correct or efficient method to follow.
Edit: and another thing, in your example, php would have already thrown an exeption or warning that the function you are trying to call does not exist, making your "function_exists" check in vain.
Lastly, if you are going to define a variable to be $bob, that could either be a variable (such as a string, integer, boolean, array, etc) OR a function itself, then what you are wanting to achieve would be correct.
However, it would STILL be an improper way of doing this, since you should actually define a $variable to be just that, a variable, and a function to be just that, a function.
If you don't want the function to exist in memory at all times, then only contruct the function within your logic area where you need it to exist .
Posted: Thu May 10, 2007 11:10 am
by volka
infolock wrote:But the problem you are missing here is that you already executed the function in that example.
Only in my example. Could also have been something like
Code: Select all
function bar($func, $dataSource) {
if (false) {
//
}
else
{
$val = validate($dataSource());
return $func($val);
}
}
foreach( pendingActions() as $tfunc) {
$results[] = bar($tfunc, 'getRecords');
}
Posted: Thu May 10, 2007 12:59 pm
by infolock
Even in that example, you would still pass the raw string for the function name instead of wasting memory by first defining a variable name.
Either way, even if you recall that same function string name 100 times, you'll have to repeat either the variable or the string.
just saying, that piece of memory could be better used elsewhere.
Posted: Thu May 10, 2007 1:50 pm
by volka
infolock wrote:Even in that example, you would still pass the raw string for the function name instead of wasting memory by first defining a variable name.
Maybe, but what's your point? That I used
$func = 'foo'; in my
example?
infolock wrote:just saying, that piece of memory could be better used elsewhere.
not necessarily. Depends on what your point really is.
Posted: Thu May 10, 2007 2:34 pm
by infolock
yes, but more so that the example is an example of a redundant and incorrect way of looking at proper development.
Again, all because you are "able" to do something a specific way, it does not make it the correct way of doing it.
My example of $i and $x should be enough to qualify as a valid point with this subject.
Holding a name for a function in a variable is redundant, and an imprroper use of system resources. There is no valid argument what-so-ever that makes putting a function declaration inside a $variable legit. The only thing that would come close would be defining a function that exists without a valid funciton name, but then you are redefining the structure and purpose of that variable. And even in this instance, it would be better to define it as a function with a name that can be refefenced and parse AT WILL LATER instead of keeping every single line associated with that function in memory at all times via a variable definition.
But defining a variable with a string representation of a function for the soul puropse of refering to a function is absolutely a waste of processor, memory and system resources.
It's just bad coding practice.
Edit:
As of this moment, I'm finished with this. I think my points are clear enough. After that, it's all up to your preference I guess. I guess if I wanted to code badly, I'd go to Ruby. (=
Posted: Thu May 10, 2007 3:32 pm
by volka
infolock wrote:But defining a variable with a string representation of a function for the soul puropse of refering to a function is absolutely a waste of processor, memory and system resources.
Sorry, definitely wrong. It's the same as (a bit cruder version of) a function pointer or an interface.
Posted: Thu May 10, 2007 3:39 pm
by volka
volka wrote:infolock wrote:But defining a variable with a string representation of a function for the soul puropse of refering to a function is absolutely a waste of processor, memory and system resources.
Sorry, definitely wrong. It's the same as (a bit cruder version of) a function pointer or an interface.
infolock wrote:yes, but more so that the example is an example of a redundant and incorrect way of looking at proper development.
McManCSU posted incomplete code snippet, I built a short example around it, doesn't need to make "real life" sense.
Posted: Thu May 10, 2007 3:50 pm
by stereofrog
infolock wrote:
But defining a variable with a string representation of a function for the soul puropse of refering to a function is absolutely a waste of processor, memory and system resources.
The classic application for function variables is a method dispatch based on parameter. The traditional approach uses a bunch of ifs or switches
Code: Select all
switch($action) {
case 'create': $this->handle_create(); break;
case 'delete': $this->handle_delete(); break;
case 'update': $this->handle_update(); break;
..... more lines
default: error("invalid action");
}
This is quite stupid and has obvious extensibility problems. The code with function variables looks much smarter:
Code: Select all
$method = "handle_$action";
if(!method_exists($this, $method))
error("invalid action");
else
$this->$method();
Hope this sheds some light on the topic.

Posted: Thu May 10, 2007 3:57 pm
by infolock
Sorry, definitely wrong. It's the same as (a bit cruder version of) a function pointer or an interface.
@volka : I think you are wrong, you think I am wrong, it's all good. My point though is that yours is not the same as a function pointer. The function pointer is already defined in the scope of php before you ever define the variable. And if you are refering to when you reference a method within an object, that assment is also incorrecct because the method in that case is used ONLY 1) after the object is instantiated and then 2) once the method is called. Either way, in that case you are dealing with a much quicker index than simply wasting the time of defining a variable to hold a string-representation of a function that is NOT availabe in memory but only after php parses it, and then does a full application scan to match a function with the said requiments.
But, we'll just agree to disagree (=
@stereofrog: WONDERFUL example! Now that is the sort of response I've been looking for all along! In that case, you are absolutely correct in your assment.
Posted: Thu May 10, 2007 4:01 pm
by volka
infolock wrote:@volka : I think you are wrong, you think I am wrong, it's all good. We'll just agree to disagree (=
There's a difference: I know you're wrong

e.g.
volka wrote:Code: Select all
foreach( pendingActions() as $tfunc) {
$results[] = bar($tfunc, 'getRecords');
}
how many functions will bar() call and how would you implement it (without the mentioned interfaces)?