Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8951619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:42:20+00:00 2026-06-15T13:42:20+00:00

I have a bit of a general dissatisfaction about the way passing by reference

  • 0

I have a bit of a general dissatisfaction about the way passing by reference works in PHP, coming from a Java background myself. This might have been talked about before on SO, so I’m sorry if this might seem redundant.

I’m going to give a code example to make things clear. Say we have a Person class:

<?php

class Person 
{
    private $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

?>

and the following usage:

<?php

require_once __DIR__ . '/Person.php';

function changeName(Person $person)
{
    $person->setName("Michael");
}

function changePerson(Person $person)
{
    $person = new Person("Jerry");
}

$person = new Person("John");

changeName($person);
echo $person->getName() . PHP_EOL;

changePerson($person);
echo $person->getName() . PHP_EOL;

?>

Now I, and many others coming from programming in Java or C# into PHP, would expect the above code to output:

Michael
Jerry

However, it does not, it outputs:

Michael
Michael

I know it can be fixed by using &. From looking into it, I understand that this is because the reference is passed into the function by value (a copy of the reference). But this to me constitutes an unexpected / inconsistent behavior, so the question would be: Is there any particular reason or benefit for which they chose to do it this way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:42:21+00:00Added an answer on June 15, 2026 at 1:42 pm

    Your code would work as expected if used this way:

    function changePerson(Person &$person) // <- & to state the reference
    {
        $person = new Person("Jerry");
    }
    
    $person = new Person("John");
    
    changeName($person);
    echo $person->getName() . PHP_EOL;
    
    changePerson($person);
    echo $person->getName() . PHP_EOL;
    

    See the output: http://codepad.org/eSt4Xcpr

    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.

    A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, 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.

    Quoted from http://php.net/manual/en/language.oop5.references.php.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit new to ColdFusion (coming from Java/OOP world) I have a custom
I'm a bit of a javascript noob, but I have a general question that
I have a question regarding how initialization vector works on cryptography. I have bit
Have a bit of confusion here. I added a few objects from a class
This is a bit of a general question. Say I have a class X
I have done a bit of testing on this myself (During the server side
I have to give a general note to some huge Java project for which
Just a bit of background first. I currently have a site hosted with Windows
I need a bit of help. I have (reference?) table with columns: id ,
I am a bit new to iOS development and have a fairly general question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.