[RFC] Explicit call-site send-by-ref syntax

  101254
December 6, 2017 19:49 nikita.ppv@gmail.com (Nikita Popov)
Hi internals,

I'd like propose optional support for explicitly marking by-reference
argument passing at the call-site, in addition to the declaration-site:

    https://wiki.php.net/rfc/explicit_send_by_ref

In short, while currently we have

    function byRef(&$ref) {...}
    byRef($var);

this proposal would also allow

    function byRef(&$ref) {...}
    byRef(&$var);

so that the use of by-reference passing is obvious without having to
consult the function declaration.

Regards,
Nikita
  101255
December 6, 2017 20:08 lists@rhsoft.net ("lists@rhsoft.net")
Am 06.12.2017 um 20:49 schrieb Nikita Popov:
> I'd like propose optional support for explicitly marking by-reference > argument passing at the call-site, in addition to the declaration-site: > > https://wiki.php.net/rfc/explicit_send_by_ref > > In short, while currently we have > > function byRef(&$ref) {...} > byRef($var); > > this proposal would also allow > > function byRef(&$ref) {...} > byRef(&$var); > > so that the use of by-reference passing is obvious without having to > consult the function declaration
IMHO a bad idea after "PHP Fatal error: Call-time pass-by-reference has been removed" not that long ago which was exactly the same syntax https://stackoverflow.com/questions/8971261/php-5-4-call-time-pass-by-reference-easy-fix-available https://stackoverflow.com/questions/18046846/why-is-function-call-by-reference-in-php-deprecated
  101256
December 6, 2017 21:11 pollita@php.net (Sara Golemon)
On Wed, Dec 6, 2017 at 2:49 PM, Nikita Popov ppv@gmail.com> wrote:
> I'd like propose optional support for explicitly marking by-reference > argument passing at the call-site, in addition to the declaration-site: > > https://wiki.php.net/rfc/explicit_send_by_ref > > In short, while currently we have > > function byRef(&$ref) {...} > byRef($var); > > this proposal would also allow > > function byRef(&$ref) {...} > byRef(&$var); > > so that the use of by-reference passing is obvious without having to > consult the function declaration. > The way I understand this, it only provides a readability hint for the
human, yes? As far as the compiler is concerned, you're either confirming what it already knows, or it's throwing an error because what you think will be pass-by-ref, won't be. I'm not strictly against it in terms of helping readability for humans, but that kind of information can also live in a docblock with nearly the same force and effect (modulo the warning when the hint is wrong). -Sara
  101257
December 6, 2017 21:14 pollita@php.net (Sara Golemon)
On Wed, Dec 6, 2017 at 4:11 PM, Sara Golemon <pollita@php.net> wrote:
>... that kind of information can also live in a docblock with > nearly the same force and effect (modulo the warning when the hint is > wrong). > Sorry, ignore that last statement. I've got a weird headache going on atm.
Still neutral on it either way. Yay for readability by humans, I guess. -Sara
  101258
December 6, 2017 21:31 rowan.collins@gmail.com (Rowan Collins)
On 06/12/2017 19:49, Nikita Popov wrote:
> Hi internals, > > I'd like propose optional support for explicitly marking by-reference > argument passing at the call-site, in addition to the declaration-site
Hi Nikita, I approve of the aims of this proposal, but I do wonder if it would be a bit awkward to reuse the syntax which people had to spend so much effort removing in PHP 5.4 (you refer to it as a PHP 4 feature, but for many people it's a much more recent memory). Aside from the frustration of "why didn't we allow it in these cases all along", I can see people being confused if it went from OK to fatal error to encouraged and maybe even mandatory. Your future scope section mentions having more explicit "out" and "inout" annotations; perhaps it would be better to skip ahead to these, with new syntax, and more immediate benefits all round. Is there a reason we can't do this right now? I imagine these working like the & annotation, with the following extra rules: 1) Parameters marked "out" or "inout" in function definitions MUST also be marked "out" or "inout" in calls to that function. 2) Parameters marked "&" in function definitions MAY be marked with "out" or "inout" in calls to that function. 3) A variable passed to an "inout" parameter would raise a notice if it was not defined before use, since it should have a value for the "in" part. 4) A variable passed to an "out" parameter would NOT raise such a notice, since the function call would be a valid initialisation. 5) An already-initialised variable passed to an "out" parameter would be set to null before calling the function. If the function never assigned to it, it would remain null in the calling scope. Rule 2 allows for better interoperability between old and new code, and I am imagining it also applying to core functions, so that this would work without pre-initialising $matches: preg_match($pattern, $string, out $matches); I'm not sure how reference-returning functions fit into this picture, and there are probably other kinks to iron out, but it seems like it would have a lot more benefits overall. Regards, -- Rowan Collins [IMSoP]
  101259
December 6, 2017 21:41 dave+php@mudsite.com (David Walker)
On Wed, Dec 6, 2017 at 2:32 PM Rowan Collins collins@gmail.com>
wrote:

> On 06/12/2017 19:49, Nikita Popov wrote: > > Hi internals, > > > > I'd like propose optional support for explicitly marking by-reference > > argument passing at the call-site, in addition to the declaration-site > > > Hi Nikita, > > I approve of the aims of this proposal, but I do wonder if it would be a > bit awkward to reuse the syntax which people had to spend so much effort > removing in PHP 5.4 (you refer to it as a PHP 4 feature, but for many > people it's a much more recent memory). Aside from the frustration of > "why didn't we allow it in these cases all along", I can see people > being confused if it went from OK to fatal error to encouraged and maybe > even mandatory. > > Your future scope section mentions having more explicit "out" and > "inout" annotations; perhaps it would be better to skip ahead to these, > with new syntax, and more immediate benefits all round. Is there a > reason we can't do this right now? > > I imagine these working like the & annotation, with the following extra > rules: > > 1) Parameters marked "out" or "inout" in function definitions MUST also > be marked "out" or "inout" in calls to that function. > 2) Parameters marked "&" in function definitions MAY be marked with > "out" or "inout" in calls to that function. > 3) A variable passed to an "inout" parameter would raise a notice if it > was not defined before use, since it should have a value for the "in" part. > 4) A variable passed to an "out" parameter would NOT raise such a > notice, since the function call would be a valid initialisation. > 5) An already-initialised variable passed to an "out" parameter would be > set to null before calling the function. If the function never assigned > to it, it would remain null in the calling scope. > > Rule 2 allows for better interoperability between old and new code, and > I am imagining it also applying to core functions, so that this would > work without pre-initialising $matches: preg_match($pattern, $string, > out $matches); > > I'm not sure how reference-returning functions fit into this picture, > and there are probably other kinks to iron out, but it seems like it > would have a lot more benefits overall. > > Regards,
Hi Nikita, I'd be more hesitant about seeing this syntax re-introdouced. Having recently been through the process of spending days to remove all the call-time by-reference fatals. Especially when the syntax proposed is generally ignored (excepting in the has &, but declaration doesn't). I do like the C-esque feel of having the declaration require a reference, and the call-side provide the reference, but the RFC lacks the warning where the declaration has reference, but call-side lacks it. However, adding this warning would probably anger everyone who did spend the time to remove all the call-time references only to now add them back in. I'd be with Rowan here. If the language is going to re-introduce old syntax as purely a visual aid, it would probably behoove us to actually look at the in/inout/out parameter types. I would be way more excited to see that play out, than having call-time-reference brought back. Cheers, -- Dave
  101260
December 7, 2017 02:04 smalyshev@gmail.com (Stanislav Malyshev)
Hi!

> I'd like propose optional support for explicitly marking by-reference > argument passing at the call-site, in addition to the declaration-site: > > https://wiki.php.net/rfc/explicit_send_by_ref > > In short, while currently we have > > function byRef(&$ref) {...} > byRef($var); > > this proposal would also allow > > function byRef(&$ref) {...} > byRef(&$var); > > so that the use of by-reference passing is obvious without having to > consult the function declaration.
We had usage of this syntax for similar, but different purpose before it was removed. Reinstating it now would be confusing, and generally not a good practice - having one syntax mean different things in different PHP versions is not good. It is not likely that we'd ever want to make it mandatory, due to the overwhelming mass of code relying on the current syntax, and without it instead of making code more clear, it would make it more confusing - if & means by-ref, does absence of it mean by-value? Nope. So you still have to check. Unless of course you rework all existing code to add & - which would be rather hard and will make it incompatible with every currently supported version of PHP. All this to achieve no other benefit but a purely cosmetic one which a good IDE could easily deliver to you for free without changing language syntax. I do not think it is worth it. -- Stas Malyshev smalyshev@gmail.com
  101295
December 10, 2017 21:41 andreas@dqxtech.net (Andreas Hennings)
On 7 December 2017 at 03:04, Stanislav Malyshev <smalyshev@gmail.com> wrote:
> > All this to achieve no other benefit but a purely cosmetic one which a > good IDE could easily deliver to you for free without changing language > syntax. I do not think it is worth it. >
I agree with Stanislav, this should be the IDE's job. PhpStorm can already show parameter name hints in function calls. It could easily add a feature to show if a parameter is by-reference. In fact I will request this as we speak :)
  101296
December 10, 2017 21:43 andreas@dqxtech.net (Andreas Hennings)
Oh wow, feature request already exists, with a link to this thread.

On 10 December 2017 at 22:41, Andreas Hennings <andreas@dqxtech.net> wrote:

> > > On 7 December 2017 at 03:04, Stanislav Malyshev <smalyshev@gmail.com> > wrote: >> >> All this to achieve no other benefit but a purely cosmetic one which a >> good IDE could easily deliver to you for free without changing language >> syntax. I do not think it is worth it. >> > > I agree with Stanislav, this should be the IDE's job. > > PhpStorm can already show parameter name hints in function calls. It could > easily add a feature to show if a parameter is by-reference. > In fact I will request this as we speak :) >
  101262
December 8, 2017 00:38 bjorn.x.larsson@telia.com (=?UTF-8?Q?Bj=c3=b6rn_Larsson?=)
Den 2017-12-06 kl. 20:49, skrev Nikita Popov:

> Hi internals, > > I'd like propose optional support for explicitly marking by-reference > argument passing at the call-site, in addition to the declaration-site: > > https://wiki.php.net/rfc/explicit_send_by_ref > > In short, while currently we have > > function byRef(&$ref) {...} > byRef($var); > > this proposal would also allow > > function byRef(&$ref) {...} > byRef(&$var); > > so that the use of by-reference passing is obvious without having to > consult the function declaration. > > Regards, > Nikita > I think this proposal has a good point about static analysers,
not just about human readability. A good static analyser is very helpful for eg migration projects and if this proposal benefits that, it's a plus. Also using same syntax for references like in other parts of the language has a value. Now expressing the same thing in two different ways, with or without & is confusing, so I think one needs to think about if old syntax should be deprecated in 8.0. Has never used PHP 4, so not confused by old PHP 4 syntax but I guess for people on this list the percentage is higher... r//Björn
  101263
December 8, 2017 01:54 lists@rhsoft.net ("lists@rhsoft.net")
Am 08.12.2017 um 01:38 schrieb Björn Larsson:
> Den 2017-12-06 kl. 20:49, skrev Nikita Popov: > >> Hi internals, >> >> I'd like propose optional support for explicitly marking by-reference >> argument passing at the call-site, in addition to the declaration-site: >> >>      https://wiki.php.net/rfc/explicit_send_by_ref >> >> In short, while currently we have >> >>      function byRef(&$ref) {...} >>      byRef($var); >> >> this proposal would also allow >> >>      function byRef(&$ref) {...} >>      byRef(&$var); >> >> so that the use of by-reference passing is obvious without having to >> consult the function declaration. >> > I think this proposal has a good point about static analysers, > not just about human readability. > > A good static analyser is very helpful for eg migration projects > and if this proposal benefits that, it's a plus. Also using same > syntax for references like in other parts of the language has a > value. > > Now expressing the same thing in two different ways, with or > without & is confusing, so I think one needs to think about if > old syntax should be deprecated in 8.0
to get this consistent that would also require change every line of code calling core functions like sort instead of sort($array) as sort($array) i doubt that the benefits could justify the BC break
  101264
December 8, 2017 04:41 php-lists@koalephant.com (Stephen Reay)
> On 8 Dec 2017, at 08:54, "lists@rhsoft.net" <lists@rhsoft.net> wrote: > > > >> Am 08.12.2017 um 01:38 schrieb Björn Larsson: >>> Den 2017-12-06 kl. 20:49, skrev Nikita Popov: >>> Hi internals, >>> >>> I'd like propose optional support for explicitly marking by-reference >>> argument passing at the call-site, in addition to the declaration-site: >>> >>> https://wiki.php.net/rfc/explicit_send_by_ref >>> >>> In short, while currently we have >>> >>> function byRef(&$ref) {...} >>> byRef($var); >>> >>> this proposal would also allow >>> >>> function byRef(&$ref) {...} >>> byRef(&$var); >>> >>> so that the use of by-reference passing is obvious without having to >>> consult the function declaration. >>> >> I think this proposal has a good point about static analysers, >> not just about human readability. >> A good static analyser is very helpful for eg migration projects >> and if this proposal benefits that, it's a plus. Also using same >> syntax for references like in other parts of the language has a >> value. >> Now expressing the same thing in two different ways, with or >> without & is confusing, so I think one needs to think about if >> old syntax should be deprecated in 8.0 > > to get this consistent that would also require change every line of code calling core functions like sort instead of sort($array) as sort($array) > > i doubt that the benefits could justify the BC break > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
I think Rowan's suggestion makes a lot of sense. There's zero bc break for existing code, but new/updated code can get the benefits of being specific about how the parameter is used. I know "references are the wrong tool for any job" is a belief held my some of the community - I'm curious if out/inout parameters solve any of their concerns about references? Cheers Stephen
  101266
December 8, 2017 07:12 me@jhdxr.com (=?utf-8?b?Q0hVIFpoYW93ZWk=?=)
On Friday, December 8, 2017 12:42 PM, "Stephen Rea" < php-lists@koalephant.com> wrote:
>> On 8 Dec 2017, at 08:54, "lists@rhsoft.net" <lists@rhsoft.net> wrote: >> >> >> >>> Am 08.12.2017 um 01:38 schrieb Björn Larsson: >>>> Den 2017-12-06 kl. 20:49, skrev Nikita Popov: >>>> Hi internals, >>>> >>>> I'd like propose optional support for explicitly marking >>>> by-reference argument passing at the call-site, in addition to the declaration-site: >>>> >>>> https://wiki.php.net/rfc/explicit_send_by_ref >>>> >>>> In short, while currently we have >>>> >>>> function byRef(&$ref) {...} >>>> byRef($var); >>>> >>>> this proposal would also allow >>>> >>>> function byRef(&$ref) {...} >>>> byRef(&$var); >>>> >>>> so that the use of by-reference passing is obvious without having to >>>> consult the function declaration. >>>> >>> I think this proposal has a good point about static analysers, not >>> just about human readability. >>> A good static analyser is very helpful for eg migration projects and >>> if this proposal benefits that, it's a plus. Also using same syntax >>> for references like in other parts of the language has a value. >>> Now expressing the same thing in two different ways, with or without >>> & is confusing, so I think one needs to think about if old syntax >>> should be deprecated in 8.0 >> >> to get this consistent that would also require change every line of >> code calling core functions like sort instead of sort($array) as >> sort($array) >> >> i doubt that the benefits could justify the BC break >> >> -- >> PHP Internals - PHP Runtime Development Mailing List To unsubscribe, >> visit: http://www.php.net/unsub.php >> > >I think Rowan's suggestion makes a lot of sense. There's zero bc break for existing code, but new/updated code can get the benefits of being specific about how the parameter is used.
Agreed, and I think those BC break change will be fine if it targets 8.0. I know people always argue that it's stupid to break the working code, but IMHO it's ok for a major version update with good reason.
  101267
December 8, 2017 10:16 lists@rhsoft.net ("lists@rhsoft.net")
Am 08.12.2017 um 05:41 schrieb Stephen Reay:
> > >> On 8 Dec 2017, at 08:54, "lists@rhsoft.net" <lists@rhsoft.net> wrote: >> >> >> >>> Am 08.12.2017 um 01:38 schrieb Björn Larsson: >>>> Den 2017-12-06 kl. 20:49, skrev Nikita Popov: >>>> Hi internals, >>>> >>>> I'd like propose optional support for explicitly marking by-reference >>>> argument passing at the call-site, in addition to the declaration-site: >>>> >>>> https://wiki.php.net/rfc/explicit_send_by_ref >>>> >>>> In short, while currently we have >>>> >>>> function byRef(&$ref) {...} >>>> byRef($var); >>>> >>>> this proposal would also allow >>>> >>>> function byRef(&$ref) {...} >>>> byRef(&$var); >>>> >>>> so that the use of by-reference passing is obvious without having to >>>> consult the function declaration. >>>> >>> I think this proposal has a good point about static analysers, >>> not just about human readability. >>> A good static analyser is very helpful for eg migration projects >>> and if this proposal benefits that, it's a plus. Also using same >>> syntax for references like in other parts of the language has a >>> value. >>> Now expressing the same thing in two different ways, with or >>> without & is confusing, so I think one needs to think about if >>> old syntax should be deprecated in 8.0 >> >> to get this consistent that would also require change every line of code calling core functions like sort instead of sort($array) as sort($array) >> >> i doubt that the benefits could justify the BC break > I think Rowan's suggestion makes a lot of sense. There's zero bc break for existing code, but new/updated code can get the benefits of being specific about how the parameter is used.
"Now expressing the same thing in two different ways, with or without & is confusing, so I think one needs to think about if old syntax should be deprecated in 8.0" would be a massive BC break and it's much bader to use a not so long existed syntax which was now changed to a fatal error - just type "php call-by-reference" in google
> I know "references are the wrong tool for any job" is a belief held my some of the community - I'm curious if out/inout parameters solve any of their concerns about references?
now because they are not bad because the syntax, they are bad fopr most usecases because you try to be smarter the PHP's copy-on-write but in most cases you won't
  101268
December 8, 2017 16:36 me@jhdxr.com ("=?utf-8?B?Q0hVIFpoYW93ZWk=?=")
On Fri, Dec 8, 2017 06:16 PM, "lists@rhsoft.net" <lists@rhsoft.net> wrote:
> >and it's much bader to use a not so long existed syntax which was now >changed to a fatal error - just type "php call-by-reference" in google > I know most people of this list using and contributing to PHP for a very long time, but I want to point out that the old "call-time pass-by-reference" was deprecated from php 5.3, which is more than 8 years ago. People with modern framework and tools are not troubled by the old and already removed syntax. The real trouble is I cannot figure out the parameter is passed by reference or value unless I go to the defination of the method.
btw, I really googled "php call-by-reference", the latest page was post on 2012, 5 years ago.
>> I know "references are the wrong tool for any job" is a belief held my some of the community - I'm curious if out/inout parameters solve any of their concerns about references? > >now because they are not bad because the syntax, they are bad fopr most >usecases because you try to be smarter the PHP's copy-on-write but in >most cases you won't If you point is pass-by-reference is bad and stupid, then it's off-topic and I suggest you should create a new RFC to abandon this feature. This RFC is aimed at making things more clear for developers and static analyzers.
  101270
December 8, 2017 16:56 lists@rhsoft.net ("lists@rhsoft.net")
Am 08.12.2017 um 17:36 schrieb CHU Zhaowei:
> On Fri, Dec 8, 2017 06:16 PM, "lists@rhsoft.net" <lists@rhsoft.net> wrote: > > > >and it's much bader to use a not so long existed syntax which was now > >changed to a fatal error - just type "php call-by-reference" in google > > > I know most people of this list using and contributing to PHP for a very > long time, but I want to point out that the old "call-time > pass-by-reference" was deprecated from php 5.3, which is more than 8 > years ago
but PHP 5.4 where it changed ot be a fatal error is not that long ago and will live many years because it's part of RHEL7
> People with modern framework and tools are not troubled by > the old and already removed syntax. The real trouble is I cannot figure > out the parameter is passed by reference or value unless I go to the > defination of the method. > btw, I really googled "php call-by-reference", the latest page was post > on 2012, 5 years ago.
you can not because people don't use proper comments myfunction /**&$var*/$x);
> If you point is pass-by-reference is bad and stupid, then it's off-topic > and I suggest you should create a new RFC to abandon this feature. This > RFC is aimed at making things more clear for developers and static > analyzers
my main point was that if you don't make it mandatory it's worth nothing and if you make it mandatory you need to bew aware that this not only affects in PHP written function but also all calls to internal functions which work wit references like sort() and so *a lot* of code written in PHP needs to be touched FRANKLY: if you re-use the syntax and make it mandatoty it's terrible while adopt code because all your existing code won't work with the next PHP version and every adopted line no longer works with the current version because it throws fatal errors you also need to consider adoption of a future PHP version which likely get a heavy impact with syntax changes where the same code has two different meanings while both throw fatal errors in a older or in the next one ________________________ to make things clearer for developers comments where invented - see above - and that most php developers don't wirte well commented and readable code is a completly different story static analyzers - well, i need yet to see one really useable for PHP code, but that#s also a different story the real issue: if the long plan is to make the call syntax mandarory it still should not re-use a syntax which was deprecated and removed years ago because the fact you will find tons of stuff refer to the old one it should really use something else and no re-use
  101271
December 8, 2017 17:55 php-lists@koalephant.com (Stephen Reay)
> On 8 Dec 2017, at 5:16 pm, lists@rhsoft.net wrote: > > "Now expressing the same thing in two different ways, with or without & is confusing, so I think one needs to think about if old syntax should be deprecated in 8.0" would be a massive BC break
That wasn’t in Rowans suggestion, it was in Björn’s response to the original message. You’re either not reading who wrote what before you reply, or you’re deliberately trying to imply a person has advocated for something they never even mentioned. Even *if* the consensus was to drop support for & references in php8 - thats a major new version, with AFAIK, literally no planned ETA, of any kind.
  101272
December 9, 2017 02:15 lists@rhsoft.net ("lists@rhsoft.net")
Am 08.12.2017 um 18:55 schrieb Stephen Reay:
>> On 8 Dec 2017, at 5:16 pm, lists@rhsoft.net <mailto:lists@rhsoft.net> >> wrote: >> >> "Now expressing the same thing in two different ways, with or without >> & is confusing, so I think one needs to think about if old syntax >> should be deprecated in 8.0" would be a massive BC break > > That wasn’t in Rowans suggestion, it was in Björn’s response to the > original message. You’re either not reading who wrote what before you > reply, or you’re deliberately trying to imply a person has advocated for > something they never even mentioned.
i responded to Björn's response and when someone than quotes me and refer to a side thread i am really not guilty
> Even *if* the consensus was to drop support for & references in php8 - > thats a major new version, with AFAIK, literally no planned ETA, of any > kind
besides that's not the topic at all and i just responded to "If you point is pass-by-reference is bad and stupid, then it's off-topic and I suggest you should create a new RFC to abandon this feature" however, "that's a major new version" is completly irrelevant in this point of time - what is relevant to think about the outcome of whatever is suggested *long before* something is even seriously considered to make things right - with the least possible BC break unless it is justified by a major benefit which makes the break worth that's somehow learning from past mistakes and avoid to repeat them in similar changes - if such discussions would have happened always before consider implementations we would not sit here with similar functions but reverse parameter order and other such "nice" things which are ugly but fix them now would introduce a lot of more troubles than it solves
  101293
December 10, 2017 13:12 johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=)
Hi,

On Mi, 2017-12-06 at 20:49 +0100, Nikita Popov wrote:
> Hi internals, > > I'd like propose optional support for explicitly marking by-reference > argument passing at the call-site, in addition to the declaration- > site: > >     https://wiki.php.net/rfc/explicit_send_by_ref >
I would rather discourage usage of references. Since PHP 7 the cost of breaking cow isn't as expensive anymore, but receiving values by value and returning by value is more idiomatic imo. Using objects can be more efficient. johannes
  101328
December 12, 2017 06:38 smalyshev@gmail.com (Stanislav Malyshev)
Hi!

> I would rather discourage usage of references. Since PHP 7 the cost of > breaking cow isn't as expensive anymore, but receiving values by value > and returning by value is more idiomatic imo. Using objects can be more > efficient.
Objects are kind of overkill when you just need a modifyable array. And copying an array when you just need to add one value to a K-size array is still not a good idea for many apps. O(n) vs O(n^2) still matters. One should definitely be careful not to overuse refs, but there are still valid cases for using them. -- Stas Malyshev smalyshev@gmail.com
  101330
December 12, 2017 07:43 johannes@schlueters.de (=?ISO-8859-1?Q?Johannes_Schl=FCter?=)
On December 12, 2017 7:38:54 AM GMT+01:00, Stanislav Malyshev <smalyshev@gmail.com> wrote:
>Hi! > >> I would rather discourage usage of references. Since PHP 7 the cost >of >> breaking cow isn't as expensive anymore, but receiving values by >value >> and returning by value is more idiomatic imo. Using objects can be >more >> efficient. > >Objects are kind of overkill when you just need a modifyable array. And >copying an array when you just need to add one value to a K-size array >is still not a good idea for many apps. O(n) vs O(n^2) still matters. >One should definitely be careful not to overuse refs, but there are >still valid cases for using them.
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. johannes
  101332
December 12, 2017 07:51 smalyshev@gmail.com (Stanislav Malyshev)
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 :) -- Stas Malyshev smalyshev@gmail.com
  101333
December 12, 2017 08:16 johannes@schlueters.de (=?ISO-8859-1?Q?Johannes_Schl=FCter?=)
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
  101335
December 12, 2017 13:45 nikita.ppv@gmail.com (Nikita Popov)
On Tue, Dec 12, 2017 at 8:43 AM, Johannes Schlüter <johannes@schlueters.de>
wrote:

> > > On December 12, 2017 7:38:54 AM GMT+01:00, Stanislav Malyshev < > smalyshev@gmail.com> wrote: > >Hi! > > > >> I would rather discourage usage of references. Since PHP 7 the cost > >of > >> breaking cow isn't as expensive anymore, but receiving values by > >value > >> and returning by value is more idiomatic imo. Using objects can be > >more > >> efficient. > > > >Objects are kind of overkill when you just need a modifyable array. And > >copying an array when you just need to add one value to a K-size array > >is still not a good idea for many apps. O(n) vs O(n^2) still matters. > >One should definitely be careful not to overuse refs, but there are > >still valid cases for using them. > > 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.
This is no longer the case as of PHP 7. PHP 7 can share values between references and non-references. Nikita
  101346
December 13, 2017 05:01 pollita@php.net (Sara Golemon)
On Tue, Dec 12, 2017 at 2:43 AM, Johannes Schlüter
<johannes@schlueters.de> wrote:
> 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. > That *was* true in PHP 5.
In PHP 7, the with_ref() version has a very slight overhead boxing the array into an IS_REF zval, then unboxing it for the call to count(). This isn't a deep (or even shallow) copy. It's one extra zval alloc and a few integer ops. -Sara
  101300
December 11, 2017 02:29 levim@php.net (Levi Morrison)
On Wed, Dec 6, 2017 at 12:49 PM, Nikita Popov ppv@gmail.com> wrote:
> Hi internals, > > I'd like propose optional support for explicitly marking by-reference > argument passing at the call-site, in addition to the declaration-site: > > https://wiki.php.net/rfc/explicit_send_by_ref > > In short, while currently we have > > function byRef(&$ref) {...} > byRef($var); > > this proposal would also allow > > function byRef(&$ref) {...} > byRef(&$var); > > so that the use of by-reference passing is obvious without having to > consult the function declaration.
I think we ought to commit to requiring the ampersand at the call site some point in the future. As others have noted it provides little benefit at the call site if it is not required. However, there is an area where this does provide value that others have not yet mentioned or thought of: `callable` parameters. public function apply(callable $f) { return $f(&$this->data); } This requires the callable to accept the argument by reference, something we cannot currently require. Of course this is rarely needed; I am merely pointing out this feature is more than a syntactic hint to humans. Based on the current discussion I would vote yes on this RFC, despite the concerns raised by others.