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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:28:30+00:00 2026-06-14T11:28:30+00:00

While practicing for certification, I came across one MCQ the question. It was as

  • 0

While practicing for certification, I came across one MCQ the question. It was as below.

Q: Consider the following script. What will it output?

<?php
$global_obj = null;
class my_class
{
   var $value;
   function my_class()
   {
      global $global_obj;
      $global_obj = &$this;
   }
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
?>

Choose Right One
Options:

  • A. 5
  • B. 10
  • C. Nothing
  • D. The constructor will throw an error
  • E. 510

I chose and B: 10 as because in my_class constructor $global_obj is being initialized by the reference of $this. By when I cross checked my answer was wrong

As a description they mentioned that

This is a really tricky one. Upon first examination, it would seem that the constructor of
my_class stores a reference to itself inside the $global_obj variable. Therefore, one would
expect that, when we later change the value of $global_obj->my_value to 10, the corresponding
value in $a would change as well. Unfortunately, the new operator does not return a reference,
but a copy of the newly created object. Therefore, the script will output 5 and the correct
answer is A.


Ya I agree the description is well enough but still I am not able to digest it as because we have clearly assigned $global_obj the reference of $this then how can be this possible? Can any one please explain in detail?

  • 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-14T11:28:31+00:00Added an answer on June 14, 2026 at 11:28 am

    we have clearly assigned $global_obj the reference of $this then how can be this possible?

    You assign the global variable within the constructor. At that time, you are referencing kind of a temporary object. Then the constructor returns a copy of that temporary object. This is that copy that $a will reference (whereas the global var still references the temporary object).

    Going OOP with php 4 is nuts. I guess you should not understand the OOP behaviour of PHP 4 but just accept it…

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

Sidebar

Related Questions

i'm practicing php at nowadays. i have one question. i see one = on
I was practicing SRM Problems in Topcoder.I came across this problem Problem Statement: Today
While looking at the source code of the asyncore module I came across this
I'm new to javascript programming. I have found below example while practicing javascript. <html>
While practicing the jxl API, i cant extract details from certain Excel sheet its
While attempting to debug a job submission script, I ended up narrowing down the
I am new to Javascript. while practicing i encounter a code regarding event handling.
While working on Hadoop Implementation in Pseudo-Distributed Operation, I found following exception of JAVA_HOME
While executing this below Dojo code the call back mehod is calling the onFailure.
While practicing different scenarios in which CSS3 pseudo-classes and selectors might come in handy,

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.