Doc #76411 [Com]: Should state that function arguments are passed by reference if objects

From: Date: Fri, 21 Apr 2023 10:26:59 +0000
Subject: Doc #76411 [Com]: Should state that function arguments are passed by reference if objects
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19591@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76411&edit=1

 ID:                 76411
 Comment by:         buzzmeta35 at gmail dot com
 Reported by:        teo8976 at gmail dot com
 Summary:            Should state that function arguments are passed by
                     reference if  objects
 Status:             Open
 Type:               Documentation Problem
 Package:            Documentation problem
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Metabuzz360 are providitech, entertainment, health, home, real estate, business, finance etc. More
info:(https://metabuzz360.com)github.com


Previous Comments:
------------------------------------------------------------------------
[2018-06-04 03:42:01] 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.

------------------------------------------------------------------------
[2018-06-03 21:49:51] 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-03 21:30:06] 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 18:56:27] 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.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=76411&edit=1


Thread (6 messages)

« previous php.doc.bugs (#19591) next »