|
Posted by Rik on 02/15/07 17:15
Michael <mischa.tbaINSERTATHERExs4all.nl> wrote:
>
>> $filtered =3D array_filter($res,create_funtion('$v,$k','return
>> $v['age']!=3D0;'));
> Well, to begin with the quotes aren't escaped.
Well, the error was that there were unescaped quotes, as I in answer to =
=
the post saying it did not work.
> Secondly, I'm wondering what sort of object is created by create_funti=
on.
> I'd say, a funtion. But is that similar to a function in any way?
Yes, it is totally similar to a function in almost every possible way, =
allthough functions created by create_function() cannot return a value b=
y =
reference. The function is just anonymous as you're probably not going t=
o =
use it again.
In this example, you could also have done:
function check_age($v,$k){
return ($v['age']!=3D0);
}
$filtered =3D array_filter($array,'check_age');
This would clutter the declared functions with one you probably will not=
=
use again, with a name that may be hard to come up with, and possible =
collisions with another fucntion should you (or someone else) want to =
declare a function with the same name.
Also possible would have been:
$callback =3D create_function('$v,$k','return ($v[\'age\']!=3D0);');
$filtered =3D array_filter($array,$callback);
Which is essentially what we've done, only split for readability, and as=
=
we capture the given name, it's possible to use the custom function agai=
n. =
This would work for instance:
<?php
$callback =3D create_function('','print("I can be called anonymously");'=
);
$callback();
?>
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|