php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74690 PHP throws reference notice when using namespaces.
Submitted: 2017-06-02 09:14 UTC Modified: 2017-08-12 00:13 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: contact at greg dot md Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 7.1.5 OS: Mac
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: contact at greg dot md
New email:
PHP Version: OS:

 

 [2017-06-02 09:14 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-02 09:53 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2017-06-02 09:53 UTC] 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 10:02 UTC] nikic@php.net
The bug here is that the notice does not occur in the non-namespaced case.
 [2017-06-02 10:07 UTC] 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 15:49 UTC] requinix@php.net
-Status: Not a bug +Status: Verified
 [2017-06-02 15:49 UTC] 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 16:35 UTC] nikic@php.net
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.
 [2017-08-12 00:13 UTC] ajf@php.net
-Status: Verified +Status: Wont fix
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 13:01:27 2024 UTC