# RFC: allow_call_pass_by_reference

- Thread: https://externals.io/message/37773
- Original archive: https://news-web.php.net/php.internals/37773
- Started by Steph Fox on 2008-05-21T13:13:04+00:00
- Messages: 20
- RFC: call_time_pass_by_reference (Implemented (PHP 5.3)) — https://externals.io/rfc/calltimebyref.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #37773 — Steph Fox — 2008-05-21T13:13:04+00:00

URL: https://externals.io/message/37773#37773

I looked into it again (and found things I didn't know before). This one's 
bugging me enough that I braved the Wiki:

http://wiki.php.net/rfc/calltimebyref

- Steph

---

## #37774 — Olivier Hill — 2008-05-21T13:17:50+00:00

URL: https://externals.io/message/37773#37774 · In reply to: #37773

Hello Steph,

Is it me, or the wiki post talks about param and return by ref?

Return by ref is not related to call-time pass by ref, is it?

Anyway, my opinion is clear on that one: shoot that dead cow. It's
been deprecated for years, and you should declare your function
prototypes with the param by ref anyway if you need it.

Regards,
Olivier


> I looked into it again (and found things I didn't know before). This one's
> bugging me enough that I braved the Wiki:

---

## #37775 — Steph Fox — 2008-05-21T13:26:39+00:00

URL: https://externals.io/message/37773#37775 · In reply to: #37774

> Is it me, or the wiki post talks about param and return by ref?
>
> Return by ref is not related to call-time pass by ref, is it?

See zend_compile.c.

> Anyway, my opinion is clear on that one: shoot that dead cow. It's
> been deprecated for years, and you should declare your function
> prototypes with the param by ref anyway if you need it.

Did you bother to read the proposal or did you just hit send? What exactly 
do you think the RFC is about?

- Steph

---

## #37776 — Steph Fox — 2008-05-21T13:41:23+00:00

URL: https://externals.io/message/37773#37776 · In reply to: #37774

For anyone else who doesn't read to the end...

> Anyway, my opinion is clear on that one: shoot that dead cow. It's
> been deprecated for years, and you should declare your function
> prototypes with the param by ref anyway if you need it.

All I'm asking is that we throw a warning by default in 5_3, because in PHP 
6 there isn't even an option to switch that warning off. To be frank this 
shouldn't ever have needed to go to an RFC; the lack of warning is an 
obvious bug in PHP which should simply be fixed.

- Steph

---

## #37781 — Hector Santos — 2008-05-21T14:12:07+00:00

URL: https://externals.io/message/37773#37781 · In reply to: #37773

Steph Fox wrote:
> I looked into it again (and found things I didn't know before). This 
> one's bugging me enough that I braved the Wiki:
> 
> http://wiki.php.net/rfc/calltimebyref
> 

It has:

| Proposal
|
| Switch allow_call_time_pass_reference off by default in PHP_5_3
| branch. At present there is no warning when running PHP under default
| settings, whereas in PHP 6 there will be a 'deprecated' warning given
| and no means of turning it off.

We will be switching it on for our extension requirements. It would be 
nice if it can be turned on in the [extension] section and the PHP 
parser would enabled it per the extension's API call only and not others.

-- 
Hector Santos

---

## #37783 — LEW21 — 2008-05-21T18:46:31+00:00

URL: https://externals.io/message/37773#37783

Sometimes call time pass by reference is useful, for example when you
want to make it possible to omit an param (normally passed by
reference) by setting null. With no call time pass by reference,
programmers are required to write:

$null = null;
foo($null);

Deleting it isn't a good idea, it should become a normal (not
deprecated) language feature.

---

## #37790 — Alexey Zakhlestin — 2008-05-22T08:05:39+00:00

URL: https://externals.io/message/37773#37790 · In reply to: #37783

> Sometimes call time pass by reference is useful, for example when you
> want to make it possible to omit an param (normally passed by
> reference) by setting null. With no call time pass by reference,
> programmers are required to write:
>
> $null = null;
> foo($null);

what stops you from declaring:

function someFunc(&$param = null)
{
}

it works just fine


> Deleting it isn't a good idea, it should become a normal (not
> deprecated) language feature.


-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #37791 — Stan Vassilev | FM — 2008-05-22T09:08:47+00:00

URL: https://externals.io/message/37773#37791 · In reply to: #37790

Hi,

There is a use case for the function allowing *explicitly* call-time pass by 
reference, because the function works both ways in subtly different ways.

I have some libraries where I had to have variations of the functions like 
"AbcByRefr()" and "Abc()", because of this inflexibility.

In a perfect world, here's how I'd do it, of course the actual syntax can be 
something else (this also improves the understanding of the code as it makes 
the intent explicit, versus implicit at call time, as it is now):

---

function foo(& $a) {} // allows only explicit pass by reference

foo($a); // illegal, error
foo(& $a); // legal, pass by reference

---

function bar($a) {} // forbids pass by reference

foo($a); // legal, pass as 'copy'
foo(& $a); // illegal, error

---

function baz(&? $a) {} // acknowledges that it functions properly and 
accepts both ways

foo($a); // legal, pass as 'copy'
foo(& $a); // legal, pass as reference

---

Regards,
Stan Vassilev


> 
> > […]
>
> what stops you from declaring:
>
> function someFunc(&$param = null)
> {
> }
>
> it works just fine
>
>
> > […]
>
>
> -- 
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/

---

## #37796 — LEW21 — 2008-05-22T10:51:31+00:00

URL: https://externals.io/message/37773#37796 · In reply to: #37790

2008/5/22, Alexey Zakhlestin <indeyets@gmail.com>:
> 
> > […]
>
>
> what stops you from declaring:
>
>  function someFunc(&$param = null)
>  {
>  }
>
>  it works just fine
And what if this param isn't the last param of this function, and next
params are required?

---

## #37797 — Alexey Zakhlestin — 2008-05-22T11:13:41+00:00

URL: https://externals.io/message/37773#37797 · In reply to: #37796

> 2008/5/22, Alexey Zakhlestin <indeyets@gmail.com>:
> > […]
> And what if this param isn't the last param of this function, and next
> params are required?
>

Then I would say "that is a bad design"

-- 
Alexey Zakhlestin
http://blog.milkfarmsoft.com/

---

## #37798 — Stan Vassilev | FM — 2008-05-22T11:22:07+00:00

URL: https://externals.io/message/37773#37798 · In reply to: #37797

Hi,

Actually PHP ignores default values on parameters followed by required ones. 
You can't fetch the default value even via Reflection.
This is easily detected at compile time so I wonder why the compiler doesn't 
warn.

Regards,
Stan Vassilev

> > […]
>
> Then I would say "that is a bad design"
>
> -- 
> Alexey Zakhlestin
> http://blog.milkfarmsoft.com/

---

## #37799 — Steph Fox — 2008-05-22T11:36:46+00:00

URL: https://externals.io/message/37773#37799 · In reply to: #37791

Hi Stan,

> There is a use case for the function allowing *explicitly* call-time pass 
> by reference, because the function works both ways in subtly different 
> ways.

This RFC isn't about whether or not this behaviour should be deprecated. It 
simply recommends that a warning be thrown by default in PHP 5.3 so that 
people will know to fix their code. The rationale for this is that there is 
ALWAYS a warning thrown in PHP 6, with no possibility to avoid that warning. 
It is also still marked deprecated in PHP 6, i.e. there is presumably a firm 
intention to remove the possibility for call-time pass-by-ref altogether.

call_time_pass_by_reference behaviour has been deprecated for 8 years plus - 
since before the PHP 4.0 release. If you're writing code that relies on that 
behaviour, either you don't care about the warning or you've never seen it. 
I suspect rather a lot of PHP users have never seen it, because by default 
that warning isn't thrown at all.

If you want to argue that there should be no warning by default, fine. But 
please try to stay on-topic.

Thanks,

- Steph

---

## #37914 — Stanislav Malyshev — 2008-05-27T06:18:25+00:00

URL: https://externals.io/message/37773#37914 · In reply to: #37791

Hi!

> In a perfect world, here's how I'd do it, of course the actual syntax 
> can be something else (this also improves the understanding of the code 
> as it makes the intent explicit, versus implicit at call time, as it is 
> now):
> 
> ---
> 
> function foo(& $a) {} // allows only explicit pass by reference
> 
> foo($a); // illegal, error
> foo(& $a); // legal, pass by reference

I'm not sure I understand what's the purpose of this. Passing by 
reference means that function may modify it's parameter. I see no reason 
why such function should be called with some special syntax, could you 
explain?
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #37933 — Stan Vassilev | FM — 2008-05-28T06:05:56+00:00

URL: https://externals.io/message/37773#37933 · In reply to: #37914

Hi,

There are three purposes:

1) At call time, nothing significes what is the caller passing, the syntax 
is the same for passing reference or passing value, so it's prone to 
confusion. You're passing something which *may* or *may not* be modified if 
taken as a reference. Quick, is the variable taken by reference here 
somefunction($hi).  A more clear version of the syntax is what I covered in 
my original email, where declaring function foo(& $foo) is a sort of 
contract that *forces* (with error) that the variable is passed by reference 
at call time, thus making the actions of the function explicit.

2) There's a broken symmetry here. While passing by reference is implicit at 
call time, at return time it's not. I need to both declare the function 
return by reference AND use "= & " to assign the results. Otherwise I get a 
copy. That's a problem I often have. There's no compiler error to warn me I 
did otherwise.

I'd rather all those be explicit( require pass/return by val, require 
pass/return by refr: "&", accepts both "&?"), and the strict cases throw 
errors when the contract is not met at both sides.

3) As I mentioned there are three use cases, of which we support two, which 
I covered in my original email.

The reason I said "in a perfect world" is because the current "hidden" pass 
by reference at call time will be incompatible with that scheme, so I doubt 
anyone would consider it.

Regards,
Stan Vassilev

> Hi!
>
> > […]
>
> I'm not sure I understand what's the purpose of this. Passing by reference 
> means that function may modify it's parameter. I see no reason why such 
> function should be called with some special syntax, could you explain?
> -- 
> Stanislav Malyshev, Zend Software Architect
> stas@zend.com   http://www.zend.com/
> (408)253-8829   MSN: stas@zend.com

---

## #37937 — Stanislav Malyshev — 2008-05-28T06:23:54+00:00

URL: https://externals.io/message/37773#37937 · In reply to: #37933

Hi!

> 1) At call time, nothing significes what is the caller passing, the 
> syntax is the same for passing reference or passing value, so it's prone 

There's no such thing as "passing reference or passing value". Same 
thing is always passed - zval. Only thing changing is does the function 
intend to modify it. That depends on function semantics.

> I'd rather all those be explicit( require pass/return by val, require 
> pass/return by refr: "&", accepts both "&?"), and the strict cases throw 
> errors when the contract is not met at both sides.

Believe me, you not, unless you want to turn PHP into C.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #37939 — Derick Rethans — 2008-05-28T06:26:25+00:00

URL: https://externals.io/message/37773#37939 · In reply to: #37773

> I looked into it again (and found things I didn't know before). This one's
> bugging me enough that I braved the Wiki:
> 
> http://wiki.php.net/rfc/calltimebyref

I'm all for it.

regards,
Derick

---

## #37989 — Derick Rethans — 2008-05-29T10:35:14+00:00

URL: https://externals.io/message/37773#37989 · In reply to: #37773

> I looked into it again (and found things I didn't know before). This one's
> bugging me enough that I braved the Wiki:
> 
> http://wiki.php.net/rfc/calltimebyref

I don't think we should get rid of it, or add a notice/message/whatever. 
Because this:

http://pastebin.com/d6e055957

could not be done without call time pass by ref right now. So unless 
that's fixed, we shouldn't deprecate it.

regards,
Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ezcomponents.org | http://xdebug.org

---

## #37993 — Marcus Boerger — 2008-05-29T12:45:02+00:00

URL: https://externals.io/message/37773#37993 · In reply to: #37773

Hello Steph,

  I am all for it: Making sure PHP's 5.3 default is OFF and issueing a warning when turning on starting with 5.3.

  marcus

Wednesday, May 21, 2008, 3:13:04 PM, you wrote:

> I looked into it again (and found things I didn't know before). This one's 
> bugging me enough that I braved the Wiki:

> http://wiki.php.net/rfc/calltimebyref

> - Steph 





Best regards,
 Marcus

---

## #38002 — Steph Fox — 2008-05-29T20:39:24+00:00

URL: https://externals.io/message/37773#38002 · In reply to: #37989

Hi Derick,

> I don't think we should get rid of it, or add a notice/message/whatever.
> Because this:
>
> http://pastebin.com/d6e055957
>
> could not be done without call time pass by ref right now. So unless
> that's fixed, we shouldn't deprecate it.

Hm. Actually those 'expected values' are wrong because 12 is itself an array 
and so isn't seen as an array element. But I take your point.

On the other hand, call_time_pass_reference already *is* deprecated and has 
been for the last 8 years, so maybe fixing PHP to allow it where it makes 
sense might be a good way to go?

Johannes mentioned `call_user_func()` on irc too:

<?php

function increment(&$var) {
    $var++;
}

$a = 0;
call_user_func('increment', $a);
echo $a;

Warning: Parameter 1 to increment() expected to be a reference, value given 
in...
0

$b = 0;
call_user_func('increment', &$b);
echo $b;

Deprecated: Call-time pass-by-reference has been deprecated; If you would 
like to pass it by reference, modify the declaration of `call_user_func()`. 
If you would like to enable call-time pass-by-reference, you can set 
allow_call_time_pass_reference to true in your INI file in...
1

The manual advises that you use `call_user_func_array()` instead - which 
doesn't throw a warning - and virtually all the user notes recommend 
variable functions as both faster and safer than `call_user_func()`. So why 
isn't _that_ deprecated if call_time_pass_reference is?

- Steph

---

## #38041 — Mark van der Velden — 2008-06-01T10:29:19+00:00

URL: https://externals.io/message/37773#38041 · In reply to: #37989

Derick Rethans wrote:
> 
> 
> > […]
> 
> I don't think we should get rid of it, or add a notice/message/whatever. 
> Because this:
> 
> http://pastebin.com/d6e055957
This pastebin expired, but is the same as:

http://pastebin.com/f1d8c08e8

and: http://bugs.php.net/bug.php?id=45126

> 
> could not be done without call time pass by ref right now. So unless 
> that's fixed, we shouldn't deprecate it.
> 
> regards,
> Derick
> 

- Mark
