Passed by Value or Reference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Passed by Value or Reference

Post by asif_phpdn »

For PHP 5 objects are passed by reference and for PHP 4 that is passed by Value. Is it correct?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Yes, although PHP5 objects are actually a handle not a reference. See docs for the difference.
(#10850)
asif_phpdn
Forum Commoner
Posts: 28
Joined: Sun Aug 26, 2007 8:50 pm

Post by asif_phpdn »

Actually there is nothing passed by reference. Everything is just passed by value. Isn't it?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Pass by reference looks like this:

Code: Select all

function foo(&$bar) {
In PHP4 objects are like other variables so when they are passed normally a copy of the object is made. In PHP5 an object variable contains a handle to the actual object. So when you pass an object variable you create a copy of the variable, but when you use it both the original and copy contain a handle to the same object.
(#10850)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

asif_phpdn wrote:Actually there is nothing passed by reference. Everything is just passed by value. Isn't it?
But in php5 the object handle is passed (by value) instead of a copy of the object. So you will actually access the same object in your function.
Post Reply