Re: Only variables can be passed by reference
| From: | Yasuo Ohgaki | Date: | Fri, 05 Dec 2014 01:07:03 +0000 |
| Subject: | Re: Only variables can be passed by reference | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79437@lists.php.net to get a copy of this message | ||
Hi all,
On Thu, Dec 4, 2014 at 5:28 PM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> I think we can get rid of this error now when literal is returned.
> The reason we have E_STRICT error is that legacy PHP didn't
> support this, I suppose.
>
> http://3v4l.org/8fISj
>
> Is it possible to allow literal as referenced parameter for PHP7?
> It's better to remove needless restrictions where it is possible.
> HHVM seems already support this.
>
> http://3v4l.org/t79rF
>
> Any comments?
>
It may be better to focus on return value from functions/methods.
There are cases that return values are better to be treated as usual
variables. For example,
<?php
function f1(&$v) {}
function f2() {$a = array(1,2,3); return $a; }
f1(f2());
?>
http://3v4l.org/2prWX
E_STRICT error here is too much. IMHO.
I think this kind of code should be allowed without errors rather than
forcing users to save return value to variable. e.g.
$v = f2();
f1($v);
Making scalar to array may be stay as it is now.
<?php
function f1(&$v) {}
function f2() {$a = array(1,2,3); return $a; }
// Both are Fatal error: Only variables can be passed by reference
f1((array)f2());
f1(array(f2()));
?>
http://3v4l.org/Ob1o2
HHVM behavior is interesting. It treats return value as usual variable, but
HHVM does not allow casts.
<?php
function f1(&$v) {}
function f2() {$a = array(1,2,3); return $a; }
f1((bool)f2());
?>
http://3v4l.org/J4Q8e
Any comments?
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net