Bug #74690 [Ver]: PHP throws reference notice when using namespaces.
| From: | nikic@php.net | Date: | Fri, 02 Jun 2017 16:35:56 +0000 |
| Subject: | Bug #74690 [Ver]: PHP throws reference notice when using namespaces. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209369@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74690&edit=1
ID: 74690
Updated by: nikic@php.net
Reported by: contact at greg dot md
Summary: PHP throws reference notice when using namespaces.
Status: Verified
Type: Bug
Package: Scripting Engine problem
Operating System: Mac
PHP Version: 7.1.5
Block user comment: N
Private report: N
New Comment:
No, the notice should occur, because call_user_func_array() does not return by reference. You are
right that it does not occur because we're effectively compiling this to a direct call without
the indirection in the non-namespaced case, but this doesn't change the fact that a notice
should be thrown. As a rule of thumb, if cuf/a behaves differently depending on whether it's in
a namespace or not, then the namespaced behavior is always correct.
That being said, I don't think we're going to fix this.
Previous Comments:
------------------------------------------------------------------------
[2017-06-02 15:49:41] requinix@php.net
More specific: the notice is only in a namespace and with an unqualified
"call_user_func_array". It does not warn outside of a namespace (or in the global
namespace) or with the fully-qualified "\call_user_func_array".
> The error is about the weird extra ampersand that is in front of the
> "&call_user_func_array" . It is complaining that you're trying to reference
> the
> return value of the function by reference.
That ampersand is required to properly use functions that return by-ref: one in the function
declaration to return its value by-ref, then another in the assignment to assign by-ref. The latter
is really more of a =& operator but with optional whitespace between the two characters.
This is all documented: https://secure.php.net/manual/en/language.references.return.php
> The bug here is that the notice does not occur in the non-namespaced case.
The notice *shouldn't* occur. call_user_func and call_user_func_array get special treatment
during compilation - a discrepancy between there and the ZEND_ASSIGN_REF handler is why there's
a notice.
If it were a regular function call then there wouldn't be any problem.
https://3v4l.org/D0Chh
------------------------------------------------------------------------
[2017-06-02 10:07:56] contact at greg dot md
It is definitely a bug.
If you will remove namespace from the example, it will work as expected and will return the
reference of the $foo in $bar, which is expected.
With that namespace at the top of the file looks like I can not return by reference using the
"&call_user_func_array".
Please do the same test with the namespace in the top of the file and without it.
------------------------------------------------------------------------
[2017-06-02 10:02:02] nikic@php.net
The bug here is that the notice does not occur in the non-namespaced case.
------------------------------------------------------------------------
[2017-06-02 09:53:34] danack@php.net
The error is about the weird extra ampersand that is in front of the
"&call_user_func_array" . It is complaining that you're trying to reference the
return value of the function by reference.
------------------------------------------------------------------------
[2017-06-02 09:14:15] contact at greg dot md
Description:
------------
When a namespace is defined in a file and you want to return a reference, the "PHP Notice:
Only variables should be assigned by reference" is thrown.
If I delete the namespace, then it works good.
Test script:
---------------
<?php
namespace Bar;
$foo = 'foo';
$bar = &call_user_func_array(function &() use (&$foo) {
return $foo;
}, []);
$bar = 'bar';
echo $foo . PHP_EOL;
Expected result:
----------------
bar
Actual result:
--------------
PHP Notice: Only variables should be assigned by reference in
/htdocs/php-app/vendor/greg-md/php-framework/test.php on line 9
PHP Stack trace:
PHP 1. {main}() /htdocs/php-app/vendor/greg-md/php-framework/test.php:0
Notice: Only variables should be assigned by reference in
/htdocs/php-app/vendor/greg-md/php-framework/test.php on line 9
Call Stack:
0.0002 353152 1. {main}() /htdocs/php-app/vendor/greg-md/php-framework/test.php:0
foo
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74690&edit=1