Issue
I just have found out that there is a function called func_get_arg
in PHP which enables developer to use variant style of getting arguments.
It seems to be very useful because number of argument can now be arbitrary, but I cannot think of any good example of using it.
What are the some examples of using this function to fully benefit its polymorphic characteristic?
Solution
I usually use func_get_args()
which is easier to use if wanting multiple arguments.
For example, to recreate PHP's max()
.
function max() {
$max = -PHP_INT_MAX;
foreach(func_get_args() as $arg) {
if ($arg > $max) {
$max = $arg;
}
}
return $max;
}
Now you can do echo max(1,5,7,3)
and get 7
.
Answered By - alex Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.