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

  • Home
  • SEARCH
  • 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 8252625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:36:33+00:00 2026-06-08T00:36:33+00:00

Possible Duplicate: In PHP can someone explain cloning vs pointer reference? According to http://php.net/manual/en/language.oop5.references.php

  • 0

Possible Duplicate:
In PHP can someone explain cloning vs pointer reference?

According to 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.

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.

If objects are passed by alias or handler then in what situation would you actually want to pass by reference?

myFunc($obj); // Pass by alias/handler

myFunc(& $obj); // Pass by reference (when would you do this?)

myFunc(clone $obj); // Create a new object

What are the different use-cases for each of these?

  • 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-08T00:36:35+00:00Added an answer on June 8, 2026 at 12:36 am

    What are the different use-cases for each of these?

    You already named the three different use-cases:

    1. Passing an object as parameter
    2. Passing an alias as parameter
    3. Passing an object clone as parameter

    Naturally you do each of these three things when you need to do the specific case. You normally only need 1. in 99.9%. You do the other 0.1% when you need to.

    An examlary use-case (as asked for in comments) for a variable alias as parameter as a little example (Demo):

    class VarFreezer
    {
        private $alias;
        private $value;
        public function __construct(&$object) {
            $this->alias = &$object;
            $this->value = $object;
        }
        public function restore()
        {
            $this->alias = $this->value;
        }
    }
    
    $var = new stdClass();
    $var->value = 'I am an object now.';
    
    $freezer = new VarFreezer($var);
    
    $var = 'I am a string now.';
    
    $freezer->restore();
    
    var_dump($var);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Using Memcache vs Memcached with PHP Someone can explain me the difference
Possible Duplicate: Does PHP have threading? I found this: http://php.net/manual/en/function.pcntl-fork.php But I can't tell
Possible Duplicate: PHP 5.4 Call-time pass-by-reference - Easy fix available? I'm currently modifying someone's
Possible Duplicate: Insert string between two points with PHP How can I replace everything
Possible Duplicate: Dirt-simple PHP templates… can this work without eval ? Let's say I
Possible Duplicate: .htaccess rewrite image file to php script How can I execute a
Possible Duplicate: How can I convert ereg expressions to preg in PHP? I need
Possible Duplicate: How can I only allow certain filetypes on upload in php? i'm
Possible Duplicate: Can anybody suggest the best image resize script in php? I'm still
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:

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.