php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76411 Should state that function arguments are passed by reference if objects
Submitted: 2018-06-03 18:56 UTC Modified: 2018-06-03 21:49 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: teo8976 at gmail dot com Assigned:
Status: Open Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2018-06-03 18:56 UTC] teo8976 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/functions.arguments
---

Where it says:
"""
By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function).
"""

it should say that an exception to this are objects, which are passed by reference.


For comparison, the documentation for the assignment operator does specify that:

http://docs.php.net/manual/en/language.operators.assignment.php
"""
Note that the assignment copies the original variable to the new one (assignment by value), [...]
An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference
"""

So, the documentation for function arguments should be equally clear and explicit.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-03 21:30 UTC] cmb@php.net
> it should say that an exception to this are objects, which are
> passed by reference.

Note, though, that this would still be misleading.  Consider

  function foo($object) {
      $object = null;
  }

vs.

  function foo(&$object) {
    $object = null;
  }

Only the latter has an effect which can be observed outside the
function; see <https://3v4l.org/04vX5>.
 [2018-06-03 21:49 UTC] requinix@php.net
The "objects are passed by reference" statement is directly addressed in the OOP docs.

http://php.net/manual/en/language.oop5.references.php
> One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default".
> This is not completely true. This section rectifies that general thought using some examples.

The terms "by value" and "by reference" refer to the concept of value-types and reference-types.
https://en.wikipedia.org/wiki/Value_type
https://en.wikipedia.org/wiki/Reference_type
Those terms are being used correctly, but because PHP also has references the "by reference" is ambiguous.
 [2018-06-04 03:42 UTC] a at b dot c dot de
The line on the Assignment Operators page is the faulty one.

Assignment of objects is by value as well (an object's value is the object itself*; that value is the one that is assigned).

What assignment does not do is create a new object with the same properties as the object being assigned (that's what clone is for).

* Which is why objects are said to be passed by value to functions. Since the value of an object is the object itself, changing the passed object's properties changes the passed object's properties.
 [2023-04-21 10:26 UTC] buzzmeta35 at gmail dot com
Metabuzz360 are providitech, entertainment, health, home, real estate, business, finance etc. More info:(https://metabuzz360.com)github.com
 [2023-05-16 16:44 UTC] praveshppc at gmail dot com
We also accept guest posts related to the latest technology, finance, business, SEO, and much more. praveshpatel.com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Oct 09 11:01:27 2024 UTC