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

php.internals

Nikita Popov

8 years ago
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

lists@rhsoft.net

8 years ago
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

Sara Golemon

8 years ago
On Wed, Dec 6, 2017 at 2:49 PM, Nikita Popov <nikita.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

Sara Golemon

8 years ago
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

Rowan Collins

8 years ago
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]

David Walker

8 years ago
On Wed, Dec 6, 2017 at 2:32 PM Rowan Collins <rowan.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

Stas Malyshev

8 years ago
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

Andreas Hennings

8 years ago
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 :)

Andreas Hennings

8 years ago
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:

Björn Larsson

8 years ago
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

lists@rhsoft.net

8 years ago
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

Stephen Reay

8 years ago
> 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

=?utf-8?B?5rGf5rmW5aSn6Jm+5LuB?=

8 years ago
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.

lists@rhsoft.net

8 years ago
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

=?utf-8?B?5rGf5rmW5aSn6Jm+5LuB?=

8 years ago
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.

lists@rhsoft.net

8 years ago
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

Stephen Reay

8 years ago
> 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.

lists@rhsoft.net

8 years ago
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

Johannes Schlueter

8 years ago
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

Stas Malyshev

8 years ago
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

Johannes Schlueter

8 years ago
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

Stas Malyshev

8 years ago
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

Johannes Schlueter

8 years ago
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

Nikita Popov

8 years ago
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

Sara Golemon

8 years ago
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

Levi Morrison

8 years ago
On Wed, Dec 6, 2017 at 12:49 PM, Nikita Popov <nikita.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.

Andrew Faulds

8 years ago
Hi Levi, Levi Morrison wrote:
> On Wed, Dec 6, 2017 at 12:49 PM, Nikita Popov <nikita.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. >
Perhaps more useful is the inverse: in future, we could require that a callable *not* take a value by-reference unless the caller asks for it. That would prevent potential monkeying with the scope of the caller by the callee — if right now you do $f($this->data), you might forget that $f could take that parameter by reference and gain the ability to modify that variable indefinitely… Thanks
-- Andrea Faulds https://ajf.me/

Nikita Popov

7 years ago
On Wed, Dec 6, 2017 at 8:49 PM Nikita Popov <nikita.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. > > Regards, > Nikita >
I've rebased and finished the implementation for this and would like to move forward with this RFC. I think it can either go forward as-is, in that it constitutes the first step towards bringing sanity to by-reference passing in the long term. Or I could first try to push through https://wiki.php.net/rfc/namespace_scoped_declares or some variant thereof so that call-site reference passing annotations can be made required on a per-library/project basis. As most of the feedback here has been on whether this is really worthwhile if it's only optional, I guess the second option would be preferred? Nikita

Girgias

7 years ago
On Thu, 25 Jul 2019 at 14:32, Nikita Popov <nikita.ppv@gmail.com> wrote:
> On Wed, Dec 6, 2017 at 8:49 PM Nikita Popov <nikita.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. > > > > Regards, > > Nikita > > > > I've rebased and finished the implementation for this and would like to > move forward with this RFC. > > I think it can either go forward as-is, in that it constitutes the first > step towards bringing sanity to by-reference passing in the long term. Or I > could first try to push through > https://wiki.php.net/rfc/namespace_scoped_declares or some variant thereof > so that call-site reference passing annotations can be made required on a > per-library/project basis. > > As most of the feedback here has been on whether this is really worthwhile > if it's only optional, I guess the second option would be preferred? > > Nikita >
I think this can go as-is and a namespace scoped declare (or something similar) could always be added after the fact to make it required to use via a declare statement. Small semi-related tangent but maybe in conjunction to the strict comparison RFC and making it mandatory to use '&' at the call site via some way, we could have some "meta" declare statement which automatically would enable strict types, mark reference at call site and the strict comparison. Just an idea which I'm not sure is that wise. Best regards George P. Banyard

Rowan Collins

7 years ago
On Thu, 25 Jul 2019 at 13:32, Nikita Popov <nikita.ppv@gmail.com> wrote:
> I think it can either go forward as-is, in that it constitutes the first > step towards bringing sanity to by-reference passing in the long term. >
Hi Nikita, As I mentioned before, I think this RFC is 10 years too late: if this goes ahead, we'll be telling a lot of people "You know all those reference annotations you removed when you upgraded to PHP 5.4? You have to put them all back again now!" To be clear, forcing a parameter to be by-reference against the signature of the called function was a bad feature, and this proposal would have been better. But without a time machine, I think this will cause more confusion than it brings value. Adding "out" and "inout" keywords, as mentioned in "future scope", is a much more powerful change, won't require any opt-in modes or breaks to existing code, and would be looking forward rather than backward. It's hard to see how tweaking the meaning of "&" will lead us closer to that, making it feel more like "alternative" than "future". (For those without threaded mail clients, the RFC in question is https://wiki.php.net/rfc/explicit_send_by_ref Incidentally, the "Proposed for" on the RFC needs updating!) Regards,
-- Rowan Collins [IMSoP]

Nikita Popov

7 years ago
On Thu, Jul 25, 2019 at 3:14 PM Rowan Collins <rowan.collins@gmail.com> wrote:
> On Thu, 25 Jul 2019 at 13:32, Nikita Popov <nikita.ppv@gmail.com> wrote: > > > I think it can either go forward as-is, in that it constitutes the first > > step towards bringing sanity to by-reference passing in the long term. > > > > > Hi Nikita, > > As I mentioned before, I think this RFC is 10 years too late: if this goes > ahead, we'll be telling a lot of people "You know all those reference > annotations you removed when you upgraded to PHP 5.4? You have to put them > all back again now!" > > To be clear, forcing a parameter to be by-reference against the signature > of the called function was a bad feature, and this proposal would have been > better. But without a time machine, I think this will cause more confusion > than it brings value. > > Adding "out" and "inout" keywords, as mentioned in "future scope", is a > much more powerful change, won't require any opt-in modes or breaks to > existing code, and would be looking forward rather than backward. It's hard > to see how tweaking the meaning of "&" will lead us closer to that, making > it feel more like "alternative" than "future". >
While the out/inout keywords are conceptually nice, I don't think they really change anything regarding the migration path. In your previous mail you mentioned that we could make these keywords required if they are used on the declaring function: But that does not solve the problem of existing functions. I think nowadays it is well known that by-reference passing is to be avoided and I don't see it particularly commonly in user code. By-reference passing is mainly used when it is needed to interact with existing by-reference functions such as preg_match(). We can hardly switch these functions to use out/inout if we require the corresponding keyword on the call-site. This proposal (in conjunction with the option to make it required) would solve the main issues I have with the by-reference passing implementation -- the out/inout approach is a refinement over that, but I'm not convinced that it a worthwhile refinement relative to the language and engine complexity it will introduce. It would be a necessary step if we had plans to eliminate references from PHP entirely, but despite how much I dislike PHP references, I don't think that eliminating references entirely is possible even on a very long time-scale. Nikita

Rowan Collins

7 years ago
On Thu, 25 Jul 2019 at 14:48, Nikita Popov <nikita.ppv@gmail.com> wrote:
>
I think nowadays it is well known that by-reference passing is to be
> avoided and I don't see it particularly commonly in user code. By-reference > passing is mainly used when it is needed to interact with existing > by-reference functions such as preg_match(). We can hardly switch these > functions to use out/inout if we require the corresponding keyword on the > call-site. >
I guess the call-site syntax would still need to be opt-in for compatibility reasons, but we could definitely mark the parameters as "out" in internal functions, even if that was mainly a documentation / reflection change. That would stop people having to write `$matches = []; preg_match($foo, $bar, $matches);` to ensure that the output parameter is initialised. I have been annoyed by that more often than I've encountered a function where I wasn't sure if the parameter was by-reference or not. This proposal (in conjunction with the option to make it required) would
> solve the main issues I have with the by-reference passing implementation >
If this remains optional, I wouldn't have much appetite for using it, because the benefit feels very slight. The fact that it wouldn't always be mandatory makes the benefit even slighter, since you still couldn't look at foo($bar) and know whether it was by-reference without also knowing what declare options were in scope.
> the out/inout approach is a refinement over that, but I'm not convinced > that it a worthwhile refinement relative to the language and engine > complexity it will introduce. >
Would it really be that complex? The only real difference between "out" and "&" would be automatically setting the variable to null when it was passed to the function. Regards,
-- Rowan Collins [IMSoP]

Nikita Popov

7 years ago
On Thu, Jul 25, 2019 at 4:41 PM Rowan Collins <rowan.collins@gmail.com> wrote:
> On Thu, 25 Jul 2019 at 14:48, Nikita Popov <nikita.ppv@gmail.com> wrote: > > > > I think nowadays it is well known that by-reference passing is to be > > avoided and I don't see it particularly commonly in user code. > By-reference > > passing is mainly used when it is needed to interact with existing > > by-reference functions such as preg_match(). We can hardly switch these > > functions to use out/inout if we require the corresponding keyword on the > > call-site. > > > > > I guess the call-site syntax would still need to be opt-in for > compatibility reasons, but we could definitely mark the parameters as "out" > in internal functions, even if that was mainly a documentation / reflection > change. > > That would stop people having to write `$matches = []; preg_match($foo, > $bar, $matches);` to ensure that the output parameter is initialised. I > have been annoyed by that more often than I've encountered a function where > I wasn't sure if the parameter was by-reference or not. >
Eww, please don't write code like that... This proposal (in conjunction with the option to make it required) would
> > solve the main issues I have with the by-reference passing implementation > > > > > If this remains optional, I wouldn't have much appetite for using it, > because the benefit feels very slight. The fact that it wouldn't always be > mandatory makes the benefit even slighter, since you still couldn't look at > foo($bar) and know whether it was by-reference without also knowing what > declare options were in scope. >
For a drive-by contribution to an open-source project? Maybe not. For anything that you want to work on seriously (say your own code or your employers code), you'll want to check the declares and then have guarantees on how the language behaves. It's somewhat off-topic, but as George mentioned in his email, this doesn't have to be an agglomeration of individual declares that are randomly flipped on and off: It could also be something like a "language level" (like editions in rust). To sick with the analogy of Rust editions, think of it as switching your project to PHP 2020 -- where passing by-reference requires a call-site annotation, use of dynamic properties throws, operators have stricter type requirements, etc... So if you see foo($x) in your code, that's definitely a by-value pass!
> > the out/inout approach is a refinement over that, but I'm not convinced > > that it a worthwhile refinement relative to the language and engine > > complexity it will introduce. > > > > > Would it really be that complex? The only real difference between "out" and > "&" would be automatically setting the variable to null when it was passed > to the function. >
That depends on how the feature is supposed to work. For me, the main point of having out/inout would be a move away from references, so this would require the implementation of an entirely new calling convention for out and inout parameters. I would expect that $z = foo($x, out $y) would translate (in terms of behavior, not actual implementation) to something like [$z, $y] = foo($x) and $z = foo($x, inout $y) to [$z, $y] = foo($x, $y) The behavior of type annotations should also change, "out T $x" should check that the value assigned to $x on function exist (or possibly on every write to the variable?) is T. "inout T $x" should check that $x is T on entry, and also on exit (or on every assignment). This would be a pretty non-trivial change. The technically hardest parts would be the changes to type-hint behavior (depending on semantic details) and making this work if call-site annotations are missing (very hard). I think we should only do this if the call-site annotations are required (still leaving the problem of old functions). Of course, that's just what I have in mind ... the alternative (and likely what you have in mind) is to make out/inout parameters basically normal by-reference parameters, with the only difference that inout uses an RW fetch instead of a W fetch and thus throws a notice if the referenced variable does not exist. That's certainly a possibility (and technically much simpler), but I also think that it squanders most of the potential behind out/inout parameters. Nikita

Rowan Collins

7 years ago
On Thu, 25 Jul 2019 at 16:18, Nikita Popov <nikita.ppv@gmail.com> wrote:
>> That would stop people having to write `$matches = []; preg_match($foo,
$bar, $matches);`
> Eww, please don't write code like that...
Huh? How would you write it then? The behavior of type annotations should also change, "out T $x" should
> check that the value assigned to $x on function exist (or possibly on every > write to the variable?) is T. "inout T $x" should check that $x is T on > entry, and also on exit (or on every assignment). >
Ah, you're right, I hadn't thought about the implications for type checks. Still, I'd rather wait for an actual new feature like this than dig into my repo and revert the PHP 5.4 update commit so that all the call-site ampersands are back, but still with all the drawbacks of references. Contrary to the RFC, I have never thought of this as "PHP 4 behaviour", because I used it right up until 5.3, exactly how this RFC is now suggesting is the future. (I guess I should have paid more attention to deprecation notices at the time!) Regards,
-- Rowan Collins [IMSoP]

Stas Malyshev

7 years ago
Hi!
> I think nowadays it is well known that by-reference passing is to be > avoided and I don't see it particularly commonly in user code. By-reference
I don't think it's true. It depends on the style of coding, of course, but there are many situations where the most convenient solution is by-ref passing, mostly when single function can change more than one thing. Of course there are ways around it, but they are less convenient and less intuitive. Additionally, if you want a function to do some mutation with arrays, there's only two ways of doing it - either returning the resulting array (which can double the data) or pass by-ref. Again, there are ways around it, but again they are less convenient. And, if your own argument recognizes internal functions have legitimate reasons to use references, the same comes for user functions too - they could implement similar functionality.
-- Stas Malyshev smalyshev@gmail.com

Marco Pivetta

7 years ago
On Sun, Jul 28, 2019, 04:47 Stanislav Malyshev <smalyshev@gmail.com> wrote:
> Hi! > > > I think nowadays it is well known that by-reference passing is to be > > avoided and I don't see it particularly commonly in user code. > By-reference > > I don't think it's true. It depends on the style of coding, of course, > but there are many situations where the most convenient solution is > by-ref passing, mostly when single function can change more than one > thing.
Nah, by-ref is pretty much avoided in OSS packages, but we can surely survey the ecosystem to verify this.

Stas Malyshev

7 years ago
Hi!
> Nah, by-ref is pretty much avoided in OSS packages, but we can surely > survey the ecosystem to verify this.
I literally work with code that uses references every day. So may be you haven't encountered it but the attitude of "nah, never happens" I think is a bit misplaced. Also please remember not all PHP code is latest composer packages. In fact, most of it isn't.
-- Stas Malyshev smalyshev@gmail.com

Marco Pivetta

7 years ago
On Sun, Jul 28, 2019 at 9:06 PM Stanislav Malyshev <smalyshev@gmail.com> wrote:
> Hi! > > > Nah, by-ref is pretty much avoided in OSS packages, but we can surely > > survey the ecosystem to verify this. > > I literally work with code that uses references every day. So may be you > haven't encountered it but the attitude of "nah, never happens" I think > is a bit misplaced. > Also please remember not all PHP code is latest composer packages. In > fact, most of it isn't. >
I do work with code like that: the teams maintaining these codebases are actively removing by-ref calls when they encounter them. Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/

Stas Malyshev

7 years ago
Hi!
> I do work with code like that: the teams maintaining these codebases are > actively removing by-ref calls when they encounter them. 
You seem to be intent on equating your personal experience with needs of every PHP developer on the planet. I can't prevent that but I can say that this experience is limited and does not match what is happening in parts of the PHP world you are not personally experiencing.
-- Stas Malyshev smalyshev@gmail.com

Claude Pache

7 years ago
> Le 28 juil. 2019 à 21:12, Marco Pivetta <ocramius@gmail.com> a écrit : > > On Sun, Jul 28, 2019 at 9:06 PM Stanislav Malyshev <smalyshev@gmail.com> > wrote: > >> Hi! >> >>> Nah, by-ref is pretty much avoided in OSS packages, but we can surely >>> survey the ecosystem to verify this. >> >> I literally work with code that uses references every day. So may be you >> haven't encountered it but the attitude of "nah, never happens" I think >> is a bit misplaced. >> Also please remember not all PHP code is latest composer packages. In >> fact, most of it isn't. >> > > I do work with code like that: the teams maintaining these codebases are > actively removing by-ref calls when they encounter them. > > Marco Pivetta
Do your teams actively remove direct calls to array_push() and array_multisort()? In any case, I’m sure that most PHP developers don’t. —Claude

Marco Pivetta

7 years ago
On Mon, Jul 29, 2019, 10:40 Claude Pache <claude.pache@gmail.com> wrote:
> > > > Le 28 juil. 2019 à 21:12, Marco Pivetta <ocramius@gmail.com> a écrit : > > > > On Sun, Jul 28, 2019 at 9:06 PM Stanislav Malyshev <smalyshev@gmail.com> > > wrote: > > > >> Hi! > >> > >>> Nah, by-ref is pretty much avoided in OSS packages, but we can surely > >>> survey the ecosystem to verify this. > >> > >> I literally work with code that uses references every day. So may be you > >> haven't encountered it but the attitude of "nah, never happens" I think > >> is a bit misplaced. > >> Also please remember not all PHP code is latest composer packages. In > >> fact, most of it isn't. > >> > > > > I do work with code like that: the teams maintaining these codebases are > > actively removing by-ref calls when they encounter them. > > > > Marco Pivetta > > Do your teams actively remove direct calls to array_push() and > array_multisort()? In any case, I’m sure that most PHP developers don’t. >
Yes: usually replaced by `[]` or wrappers of those functions that de-reference the variables, so that no references are used.

Nikita Popov

7 years ago
On Mon, Jul 29, 2019 at 10:40 AM Claude Pache <claude.pache@gmail.com> wrote:
> > > > Le 28 juil. 2019 à 21:12, Marco Pivetta <ocramius@gmail.com> a écrit : > > > > On Sun, Jul 28, 2019 at 9:06 PM Stanislav Malyshev <smalyshev@gmail.com> > > wrote: > > > >> Hi! > >> > >>> Nah, by-ref is pretty much avoided in OSS packages, but we can surely > >>> survey the ecosystem to verify this. > >> > >> I literally work with code that uses references every day. So may be you > >> haven't encountered it but the attitude of "nah, never happens" I think > >> is a bit misplaced. > >> Also please remember not all PHP code is latest composer packages. In > >> fact, most of it isn't. > >> > > > > I do work with code like that: the teams maintaining these codebases are > > actively removing by-ref calls when they encounter them. > > > > Marco Pivetta > > Do your teams actively remove direct calls to array_push() and > array_multisort()? In any case, I’m sure that most PHP developers don’t. >
This discussion seems to have lost track of the context ... the original quote (which Stas cherry-picked in a way that lost the original meaning):
> I think nowadays it is well known that by-reference passing is to be
avoided and I don't see it particularly commonly in user code. By-reference passing is mainly used when it is needed to interact with existing by-reference functions such as preg_match(). We can hardly switch these functions to use out/inout if we require the corresponding keyword on the call-site. You seem to be agreeing with what I originally said: That by-reference passing is mainly useful to interoperate with by-reference internal functions, which don't exactly leave you with a choice. --- In any case, I think the popularity of by-reference passing ultimately doesn't really matter much: By-reference passing still exists, will continue to exist and we have to deal with it. Which is also why I think that the question of out/inout parameters is quite orthogonal to this proposal: The problem I'm trying to address is that currently, both humans, static analyzers and the engine have a hard time telling whether an argument is going to be passed by reference (or otherwise indirectly modified) or not. Humans may use API familiarity to help them, static analyzers can (unreliably) try to infer this through global analysis, while the engine is entirely out of luck apart from the simplest of cases. This proposal (at least combined with a declare that enforces use of call-site annotations) addresses that concern. out/inout parameters do not address this concern, because they are an mechanism that would be used *in addition* to normal by-ref passing, and as such not address any problems it has. The only way I see in which out/inout would actually address the concern of this proposal is if out/inout were used instead of & at the call-site, but the parameter were still a normal reference. I don't think that's a good idea because it breaks symmetry between arguments and parameters, and also squanders any future potential to use out/inout as a way to reduce reference-use in this context. Does that make sense, Rowan? To put is more compactly, what I want is an eventual state where given $fn($a) I have a guarantee that $a is not going to be magically modified, without having to perform any global reasoning about what $fn may refer to. If an alternative proposal does not result in this eventual state, then it is not an alternative to this proposal (but possibly still a valuable addition in itself). --- Bob has brought up another interesting issue: This proposal currently does not address the case of foo(...$a), where references into $a may be added if foo() has by-reference parameters. The suggestion was to use foo(&...$a) -- however in this case only to *allow* the use of references, not require it (some args may be by-val while others may be by-ref). I'm not sure whether I like that idea or not. I think there's value in making it clear that an unpack may be by reference, but at the same time I find the discrepancy between foo(&$a) (*must* be ref) and foo(&...$a) (*may* be ref) somewhat confusing. Regards, Nikita

Stas Malyshev

7 years ago
Hi!
>> In short, while currently we have >> >> function byRef(&$ref) {...} >> byRef($var); >> >> this proposal would also allow >> >> function byRef(&$ref) {...} >> byRef(&$var);
I am not sure why it is necessary. If you call a function, one would assume you know what it is doing, at least you know its definition, and with any modern IDE if you do not, it is a matter of one click (or less) to reveal what the function does. So what is the point to mark it again? Does not seem to add anything that the writer or reader doesn't know or could easily find out. It could also imply that if the call site does not bear & mark, then the parameter can not be modified - which is not true (or only true for primitive values) and would add to the confusion.
> I think it can either go forward as-is, in that it constitutes the first > step towards bringing sanity to by-reference passing in the long term. Or I
Could you explain what you mean by "bringing sanity to by-reference passing"? Maybe if it would be clearer what you're getting at, in that context this proposal would be more understandable.
> could first try to push through > https://wiki.php.net/rfc/namespace_scoped_declares or some variant thereof > so that call-site reference passing annotations can be made required on a > per-library/project basis.
That certainly would be very annoying and I would be very much against it (and in general against the idea of fragmenting the language into a thousand of tiny pieces with different syntax by adding more and more local syntax-changing options) but I think this should be discussed separately so I'll say no more here.
-- Stas Malyshev smalyshev@gmail.com

Björn Larsson

7 years ago
Den 2019-07-25 kl. 14:32, skrev Nikita Popov:
> On Wed, Dec 6, 2017 at 8:49 PM Nikita Popov <nikita.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. >> >> Regards, >> Nikita >> > I've rebased and finished the implementation for this and would like to > move forward with this RFC. > > I think it can either go forward as-is, in that it constitutes the first > step towards bringing sanity to by-reference passing in the long term. Or I > could first try to push through > https://wiki.php.net/rfc/namespace_scoped_declares or some variant thereof > so that call-site reference passing annotations can be made required on a > per-library/project basis. > > As most of the feedback here has been on whether this is really worthwhile > if it's only optional, I guess the second option would be preferred? > > Nikita
Hi, I like this proposal since it gives the programmer one more tool to improve readability and thereby quality of code. It will also contribute to make PHP faster. So the argument that one shouldn't encourage usage of references by introducing this, will then hinder potential performance improvements. The decision about how to make Namespace scoped declares can come later, still the ongoing discussion is valuable. r//Björn L

Nicolas Grekas

7 years ago
Le mer. 6 déc. 2017 à 20:50, Nikita Popov <nikita.ppv@gmail.com> a écrit :
> 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 fully support this RFC. I've been caught many times forgetting about the fact some argument was modified by reference. Putting this information on the call side would definitely make code faster to decipher. References are part of the language. The fact that some think they should be avoided is orthogonal to the proposal. It's good to improve them. Also I think enough time has passed since php4's call-by-ref for the syntax to be reused now. I think it's unfair to call the RFC a reminiscent of call-by-ref BTW. About requiring such call-annotations using a "declare" directive it feels like a heated topic. But we don't need it, so let's split it appart. I could very well see userland tools enforce it at the CS-checking level. That would provide 99.999% of the target benefit while saving us some mental health issues and some unneeded technical challenges to solve. My 2 cts, Nicolas

Rowan Collins

7 years ago
On Tue, 30 Jul 2019 at 07:14, Nicolas Grekas <nicolas.grekas@gmail.com> wrote:
> I think enough time has passed since php4's call-by-ref for the syntax to > be > reused now. I think it's unfair to call the RFC a reminiscent of > call-by-ref BTW. >
Firstly, please let's stop calling this a "PHP 4" feature. It was fully supported right up to 5.2, deprecated in 5.3, and only removed in 5.4. Secondly, I completely disagree that it's unfair to compare the two. The syntax being proposed in this RFC (& at both call-site and definition-site) was supported by versions prior to PHP 5.4. Those versions left it optional at either side, which was certainly a mistake; but it was perfectly possible (and probably common) for coding standards to require it in both locations. It might be a good idea to include a more thorough discussion of this history in the RFC.
> About requiring such call-annotations using a "declare" directive it feels > like a heated topic. But we don't need it, so let's split it appart. I > could very well see userland tools enforce it at the CS-checking level. > That would provide 99.999% of the target benefit >
Unfortunately, it doesn't bring any of the benefits to static analysis that Nikita is proposing. Indeed, it relies on existing static analysers correctly finding the function definition to complain whether the optional & is in place. Including it as optional would certainly bring some benefit to readers, but I think it's a lot less than "99.999%" of what making it mandatory would bring. Regards,
-- Rowan Collins [IMSoP]

Nicolas Grekas

7 years ago
Le mar. 30 juil. 2019 à 10:34, Rowan Collins <rowan.collins@gmail.com> a écrit :
> On Tue, 30 Jul 2019 at 07:14, Nicolas Grekas <nicolas.grekas@gmail.com> > wrote: > > > I think enough time has passed since php4's call-by-ref for the syntax to > > be > > reused now. I think it's unfair to call the RFC a reminiscent of > > call-by-ref BTW. > > > > > Firstly, please let's stop calling this a "PHP 4" feature. It was fully > supported right up to 5.2, deprecated in 5.3, and only removed in 5.4. > > Secondly, I completely disagree that it's unfair to compare the two. The > syntax being proposed in this RFC (& at both call-site and definition-site) > was supported by versions prior to PHP 5.4. Those versions left it optional > at either side, which was certainly a mistake; but it was perfectly > possible (and probably common) for coding standards to require it in both > locations. > > It might be a good idea to include a more thorough discussion of this > history in the RFC. >
Call-time pass-by-reference is deprecated since PHP 4.3.0 and triggers a deprecation warning since then: https://3v4l.org/MFXsJ That's since Dec 2002.

Rowan Collins

7 years ago
On Tue, 30 Jul 2019 at 10:00, Nicolas Grekas <nicolas.grekas+php@gmail.com> wrote:
> Call-time pass-by-reference is deprecated since PHP 4.3.0 and triggers a > deprecation warning since then: > https://3v4l.org/MFXsJ > > That's since Dec 2002. >
It looks like the history is more complicated than either of us are remembering. I based my assertion on the migration notes for PHP 5.3 [1] which simply say:
> Call-time pass-by-reference is now deprecated.
However, that looks to have been the result of an RFC [2] which gave more background. According to that page, it's actually a PHP 3 feature, retained through PHP 4 and PHP 5, with an option to disable it (at the time of that RFC, there was still going to be a PHP 6). But since no warning was issued if you left the option in its default state, most users had no idea that it was considered deprecated, and carried on using it. So the first time many people heard that they needed to stop marking their call sites was PHP 5.3, released in 2009 and EOL in August 2014. [1] https://www.php.net/manual/en/migration53.deprecated.php [2] https://wiki.php.net/rfc/calltimebyref Regards,
-- Rowan Collins [IMSoP]

Nikita Popov

7 years ago
On Tue, Jul 30, 2019 at 11:01 AM Nicolas Grekas < nicolas.grekas+php@gmail.com> wrote:
> Le mar. 30 juil. 2019 à 10:34, Rowan Collins <rowan.collins@gmail.com> a > écrit : > > > On Tue, 30 Jul 2019 at 07:14, Nicolas Grekas <nicolas.grekas@gmail.com> > > wrote: > > > > > I think enough time has passed since php4's call-by-ref for the syntax > to > > > be > > > reused now. I think it's unfair to call the RFC a reminiscent of > > > call-by-ref BTW. > > > > > > > > > Firstly, please let's stop calling this a "PHP 4" feature. It was fully > > supported right up to 5.2, deprecated in 5.3, and only removed in 5.4. > > > > Secondly, I completely disagree that it's unfair to compare the two. The > > syntax being proposed in this RFC (& at both call-site and > definition-site) > > was supported by versions prior to PHP 5.4. Those versions left it > optional > > at either side, which was certainly a mistake; but it was perfectly > > possible (and probably common) for coding standards to require it in both > > locations. > > > > It might be a good idea to include a more thorough discussion of this > > history in the RFC. > > > > Call-time pass-by-reference is deprecated since PHP 4.3.0 and triggers a > deprecation warning since then: > https://3v4l.org/MFXsJ > > That's since Dec 2002. >
Disclaimer: I started using PHP somewhere around PHP 5.2 times and have never encountered call-time pass-by-reference as anything but a historical concern, so I'm finding it somewhat hard to empathize with the associated emotional baggage the issue may have. With that in mind, I don't see an issue with reusing the previous call-time pass-by-ref syntax here. The & at the call-site still means that the value is going to be passed by refrence (or error), so it's not like someone who was around during the call-time pass-by-ref times would misunderstand what the code does based on their prior knowledge. The only concern I see here is the emotional issue: Bringing & back as a call-site annotation is an admission that the original migration of by-ref passing has been badly botched: Yes, you removed those & at the call-site for nothing. Yes, you shouldn't have been forced to do that! Mistakes happen and hindsight is 20/20. But we should own up to those mistakes. Regards, Nikita

Rowan Collins

7 years ago
On Tue, 30 Jul 2019 at 11:28, Nikita Popov <nikita.ppv@gmail.com> wrote:
> With that in mind, I don't see an issue with reusing the previous > call-time pass-by-ref syntax here. The & at the call-site still means that > the value is going to be passed by refrence (or error), so it's not like > someone who was around during the call-time pass-by-ref times would > misunderstand what the code does based on their prior knowledge. > > The only concern I see here is the emotional issue: Bringing & back as a > call-site annotation is an admission that the original migration of by-ref > passing has been badly botched: Yes, you removed those & at the call-site > for nothing. Yes, you shouldn't have been forced to do that! > > Mistakes happen and hindsight is 20/20. But we should own up to those > mistakes. >
I think that's a reasonable summary. Could you add a short section to the RFC just acknowledging that history, and clarifying that this functionality is effectively a subset of the previously removed feature, but without its problems? You're right that it's more of an emotional reaction than a rational one, and shouldn't necessarily be a show-stopper IF we agree the proposed behaviour is a big enough gain. Regards,
-- Rowan Collins [IMSoP]