I cant seem to get any consistent info on this. Different sources appear to say different things and trhe venerable php.net iteslf (appears) not to explicitly state this - although I must admit, I only had a quick look.

In cases where I am passing around 'heavy' objects, I need to pass by reference, but I dont want to keep typing:

function foo(TypeName& $obj)

if I can get away with simply,

function foo(TypeName $obj)

So what does the standard say?

Accepted Answer

Objects are passed (and assigned) by reference. No need to use address of operator.

Granted what I typed is an oversimplification but will suit your purposes. The documentation states:

One of the key-points of PHP5 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.

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

Written by webbiedave
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki