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 467355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:34:20+00:00 2026-05-12T23:34:20+00:00

A few years ago I used to work with PHP and there’ve been many

  • 0

A few years ago I used to work with PHP and there’ve been many changes in PHP, especially on the OOP part.

Back then it was necessary to use the & operator at several places, especially when working with object instances (OOP was very rudimentary then) to avoid copying object instances.

With PHP 5.2 there seems to be a lot of improvement compared to 4.0 and I’m a little bit confused.

When do I have to use the & operator in PHP to avoid any unpleasant surprises from a Java programmer’s perspective?

Examples:

$a = (&)new MyClass();    //Necessary?

function factoryMethod() {
  $a = (&)new MyClass();  //Necessary?
  return (&) $a;          //Necessary?
}

// Other cases?!

EDIT: By chance my examples don’t need the & operator at all. But where would I need it?

  • 1 1 Answer
  • 2 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-05-12T23:34:21+00:00Added an answer on May 12, 2026 at 11:34 pm

    To make it short: nowhere in your examples.

    In case we’re dealing with objects you do not have to use the reference operator at all.

    If you’re dealing with other datatypes the most common reference operations are:

    Passing by Reference

    function foo(&$var)
    {
        $var++;
    }
    
    $a=5;
    foo($a);
    // $a is 6 here
    

    Returning References

    class foo {
        public $value = 42;
    
        public function &getValue() {
            return $this->value;
        }
    }
    
    $obj = new foo;
    $myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
    $obj->value = 2;
    echo $myValue; // prints the new value of $obj->value, i.e. 2.
    

    Looping with foreach

    $arr = array(1, 2, 3, 4);
    foreach ($arr as &$value) {
        $value = $value * 2;
    }
    // $arr is now array(2, 4, 6, 8)
    unset($value); // break the reference with the last element
    

    Be careful as the last unset()-call is important if your code uses $value again downstream.

    Whereas the first case (passing by reference) is much more common than the second case, e.g. especially when dealing with arrays:

    $array = array(1, 2, 3, 4);
    function modifyArray(&$data)
    {
        unset($data[1]);
    }
    modifyArrax($array);
    // $array = array(0 => 1, 2 => 3, 3 => 4)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The SQL statement below used to work a few years ago, now it doesn't
I used the Report Viewer Control with SQL 2005 a few years ago. Back
I used to write PHP 10 years ago. For the last few years I've
A few years ago I did a lot of work with CGI.pm. I'm evaluating
A few years ago I used the Hibernate ORM for mapping database data to
A few years ago I worked on a project where we used TIBCO Rendezvous
A few years ago I installed a CMS which was written in PHP. This
I used Python a few years ago when 2 was the only version. Now
A few years ago in Oracle 10 or 9 I used a function which
Progress Version: 10.0B05.32 A few years ago, we used the Progress Proxy Generator to

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.