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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:41:06+00:00 2026-06-17T08:41:06+00:00

The comments indicate that I have to clarify my point: I’m a developer and

  • 0

The comments indicate that I have to clarify my point: I’m a developer and consultant working on foreign code here. My current assignment is to help migrating an existing (very large) system from PHP4 to Java. While reviewing the code I stumbled across a piece of code that quite confuses me.

Some class (let’s call it TestClass) in the system I’m working on does something like this:

function reload(){
    $newobject = new TestClass();
    $this = $newobject;
}

I gave it a quick try, expecting some inconsistent behavior between accessing members of the object directly or via some getter in case this somehow works. I thought that at worst the external pointers to the existing object and the internal one (aka $this) would target different portions of the heap.
Quick tests seem to indicate that simply nothing happens.

Any idea if this has any (side) effect whatsoever?

Here is my complete test:

<?php

ini_set('display_errors', true);

class TestClass{

    var $var1;

    function TestClass(){
        $this->var1 = 0;
    }

    function getVar1(){
        return $this->var1;
    }

    function setVar1($value){
        $this->var1 = $value;
    }

    function reload(){
        $newobject = new TestClass();
        $this = &$newobject;
    }
}

echo "<h3>Creating TestObject</h3>";

$testObject = new TestClass();

echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";

echo "Setting member directly to 1<br>";

$testObject->var1 = 1;

echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";

echo "<h3>Calling reload</h3>";

$testObject->reload();

echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";
?>

I expected to get 1 and 0 at last two calls if $this now pointed towards the new object while $testObject would still point to the original one, but 1 is returned in both cases. So I need to know whether the reassignment of $this is doing anything but being a bad idea. If not I can dump that part for good. So if anyone knows of any side effects please tell.

PS: I am fully aware that the above code is not using visibility and such. The original system is written in PHP4 so why bother 😐

  • 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-17T08:41:07+00:00Added an answer on June 17, 2026 at 8:41 am

    Okay, with the hint of a colleague I got it. If the reload function is changed to

    function reload(){
        $newobject = new TestClass();
        $this = $newobject;
    }
    

    the object is overwritten with the content of the newly created one. This is due to the copy on write mentioned by “i put on my robe an wizard hat” in the comment above. If used with the forced call-by-reference nothing happens which is good.

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

Sidebar

Related Questions

In the comments on this answer , Remou writes that CurrentDB.Execute insert sql here
I use the django comments from contrib and I have an object (entry) that
I have facebook comments on my site. I noticed that when there are 10
I have code that looks like this: var ds = new DataSet(); var fooIDToFoo
I have the following code that runs whenever you click the Start button on
IEEE floating point numbers have a bit assigned to indicate the sign, which means
Comments in the SKPaymentQueue.h seem to indicate that the USER will be asked to
I am using the following list layout for items that have associated comments .
Im working with jQuery. I have an app that makes ajax requests to server
I understand that floating point calculations have accuracy issues and there are plenty of

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.