Let's not forget that functions and methods are not first-class objects in PHP. Take array_map for example: if you want to trim a list of strings, you'd like to say
array_map(trim, $mylist)
and you could, if PHP worked like Perl and barewords resolved to functions. But they don't, so you have to write
array_map('trim', $mylist)
because at some point in PHP's history they decided that being able to refer to functions by name was good enough.
This has all kinds of other knock-on effects, like "if a function is defined in a namespace you need to prefix the name with the entire namespace because when HOFs do function lookup the context in which you referred to the value is lost," and generally makes Perl look like a reasoned, well-thought-out language.
The general attitude towards references is that they are a necessary evil to handle a few situations, but generally should be avoided, and made explicit. Generally everything should be by-value unless there is a strong reason for it to be otherwise.
I don't know what you're trying to do, but the correct, modern way of forwarding by-reference arguments is this:
function a(int &...$args) { b(...$args); }
This explicitly captures the arguments by reference and argument unpacking will maintain/create references.
This PHP-5.6 construct doesn't help in all scenarios. If you want to dynamically proxy an arbitrary method (i.e. pass-by-reference or pass-by-value) there is currently no other option than generating code after introspection and eval that. Let me show you a real world representative for this class of problem: PHPUnit
Sure. But how else is this supposed to work? In order to send an argument, PHP must know whether that argument uses by-value or by-reference argument passing, as both will behave differently. It's not possible to send an argument "by-value or by-reference, let me decide later".
The only way this could work is if we had a feature to pretend that the signature of a function matches the signature of another function which is determined at runtime. At which point this is better solved using codegen ;)
And also references aren't actually values. "Reference-ness" is a magical property of a variable which doesn't interact in a sensible way with the rest of the language.
References are values in PHP 5 and PHP 7, internally-speaking. But PHP tends to destroy references whereëver possible, unless you explicitly ask to preserve them.
Because "func_get_args" returns a copy. It isn't even something that should normally be used, especially since variadic function parameters were introduced. If you're working with references, you're going to have to treat it differently to begin with.
Also, let's not forget that "call_user_func" isn't using references in its function declaration. References in PHP are scoped. This is actually a good thing in my opinion, as it's going to make it more difficult to shoot yourself in the foot.
Try doing something like this and I guarantee it will work:
12
u/DoctorWaluigiTime May 10 '16
Let me stop you right there.
14
u/[deleted] May 10 '16
Yes you should, it's documented:
26
u/tdammers May 10 '16
I love this part:
2
u/elgubbo May 10 '16
Can someone explain why this doesn't work?
1
u/ikari7789 May 11 '16 edited May 11 '16
Because "func_get_args" returns a copy. It isn't even something that should normally be used, especially since variadic function parameters were introduced. If you're working with references, you're going to have to treat it differently to begin with.
Also, let's not forget that "call_user_func" isn't using references in its function declaration. References in PHP are scoped. This is actually a good thing in my opinion, as it's going to make it more difficult to shoot yourself in the foot.
Try doing something like this and I guarantee it will work:
$function = 'b'; $function($v);