Re: [RFC] Explicit call-site send-by-ref syntax
| From: | Johannes Schlüter | Date: | Tue, 12 Dec 2017 08:16:08 +0000 |
| Subject: | Re: [RFC] Explicit call-site send-by-ref syntax | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101333@lists.php.net to get a copy of this message | ||
On December 12, 2017 8:51:42 AM GMT+01:00, Stanislav Malyshev <smalyshev@gmail.com> wrote:
>Hi!
>
>> The issue, as you well know, is that references disable
>copy-on-write. Thus assume you have code like this:
>>
>> function with_ref(&$a) {
>> count ($a);
>> }
>>
>> function no_ref($a) {
>> count($a);
>> }
>>
>> The count in with_ref() will copy the array, while no_ref() can use
>copy on write and won't actually copy.
>
>Yes, this is an issue, and it'd be good to find a way to solve it. At
>least for count() and other "pure" (however pure can it be in PHP)
>functions it seems possible. But do not think "not using references
>ever" qualifies as a solution :)
For this case there is a good solution: Let the engine be smart and pass by value :-D
And yes there are a few cases where references might be better: Graph like structures (while
I'd claim objects are nicer, but that's subjective), capturing by-ref in closures
(
use clause, while many times an object to hold state can be, subjectively, better, but
sometimes you just need a counter or such) and returning error codes by-ref (if objects or
exceptions aren't better, this most often is more low-level stuff, i.e. in json_decode()
I'd see benefits over json_error_last())
Some years back I spent quite some time with different cases almost always removing the references
gave faster and clearer code (while this proposal to add & to the call sign takes away some wtf)
not only in my opinion, but also the respective maintainers. Of course with PHP 7 the maths changed
a bit, but fundamentally I stand by my opinion.
johannes