php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #29877 variable assign by reference works even when methods don't return by reference
Submitted: 2004-08-28 22:56 UTC Modified: 2005-07-14 11:56 UTC
From: bharat at menalto dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4CVS, 5CVS (2004-12-10) OS: FreeBSD 4.8
Private report: No CVE-ID: None
 [2004-08-28 22:56 UTC] bharat at menalto dot com
Description:
------------
According to the documentation here:
http://us4.php.net/manual/en/language.references.return.php

In order to assign a variable as a reference to a value returned by a class method, the method needs to also return by reference.  However, on PHP 4.3.8 in my test script I've found that the semantics are the same whether or not you return by reference.  

This is surprising to me.  I could have sworn that it didn't used to be this way.  Please let me know if you need more information.  I can reproduce this with a stock php.ini.

If this is the desired behaviour in this case, then we should update the documentation to reflect that the ampersand is no longer necessary on the getter method.

Reproduce code:
---------------
http://www.menalto.com/.outgoing/php/ref.php

Expected result:
----------------
The URL above indicates the expected results.  I can elaborate if necessary.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-12 16:29 UTC] sniper@php.net
Note: Tested with 4.2.2 and it works exactly the same.
IMO this is not a bug.

 [2004-12-13 09:09 UTC] bharat at menalto dot com
Jani,

I appreciate that this behaviour hasn't changed in a while.  If it's part of the language, I'm ok with it.  However, in the documentation that I referenced, it clearly states:

---
Note:  Unlike parameter passing, here you have to use & in both places - to indicate that you return by-reference, not a copy as usual, and to indicate that reference binding, rather than usual assignment, should be done for $foo.
---

But as my sample code indicates (unless you can demonstrate a bug in my code), the & is not required on the function.  So if this is the desired behaviour, then let us please update the documentation to state that it is NOT required that there be an & on the function for you to get back a reference.  Either way we resolve this, there's a discrepancy that should be removed.  

Thanks!
 [2004-12-13 09:47 UTC] vrana@php.net
There's a small mistake in the sample script but it doesn't affect the issue: There should be $test2->_data instead of $test1->_data on the last line.

It's worth noting that if get() method returns literal instead of variable, PHP doesn't issue any warning and the variable 
is not bound.
 [2004-12-13 09:54 UTC] bharat at menalto dot com
I left the old script with the bug intact, but have created a second one with the fix that Jakub suggests, here:

http://www.menalto.com/.outgoing/php/ref2.php
 [2004-12-14 02:59 UTC] alindeman@php.net
This is definitely not how the current documentation describes how references work.  Does this require a trivial documentation change, or is it something in the engine/language that is not copasetic?
 [2005-04-07 16:27 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"& by function definition is optional in class methods."
 [2005-07-14 11:56 UTC] vrana@php.net
If $_data is initialized through "var $_data" and not in constructor, get() doesn't return a reference.

This inconsistency is fixed in PHP 5.1.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 20:01:31 2024 UTC