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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:51:05+00:00 2026-06-07T15:51:05+00:00

Plz tell me where i’m doing wrong… I’ve 3 classes. These are like this..

  • 0

Plz tell me where i’m doing wrong…

I’ve 3 classes. These are like this..

  1. Singleton class where i’m following singleton design pattern
  2. Class ram
  3. Class sam

In ‘ram’ class i’m setting data for singleton class object.

Now, in ‘sam’ class.. i’m trying to access singleton class object inside show_data() function of sam class.

When, i’m using..

Print_r($this) : showing empty object

but, when i’m using following code..

$singleton_obj = Singleton::getInstance();
print_r($singleton_obj); : Showing content of singleton object

My question is,
why in-case of Print_r($this) it’s showing empty object. Is there any way, i can get content of singleton class object by using Print_r($this).

MY class file is this..

<?php 
class Singleton
{
 // A static property to hold the single instance of the class
private static $instance;

// The constructor is private so that outside code cannot instantiate
public function __construct() { }

// All code that needs to get and instance of the class should call
// this function like so: $db = Database::getInstance();
public function getInstance()
{
  // If there is no instance, create one
  if (!isset(self::$instance)) {
    $c = __CLASS__;
    self::$instance = new $c;
  }
  return self::$instance;
}

// Block the clone method
private function __clone() {}


// Function for inserting data to object
public function insertData($param, $element)
{
$this->{$param} = $element;
}
}

//---CLASS ram---
class ram
{
    function __construct() 
    {
        $db = Singleton::getInstance();
        $db->insertData('name', 'Suresh');
    }
}

$obj_ram = new ram;

//---CLASS sam---
class sam extends Singleton
{
    function __construct()
    {
        parent::__construct();
   }

   public function show_data()
   {
    echo "<br>Data in current object<br>";
    print_r($this);

    echo "<br><br>Data in singleton object<br>";
        $singleton_obj = Singleton::getInstance();
        print_r($singleton_obj);
   } 
}

$obj_sam = new sam;
echo $obj_sam->show_data(); 
?>
  • 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-07T15:51:07+00:00Added an answer on June 7, 2026 at 3:51 pm

    You are creating a “sam” object by “new sam“, you sould use “sam::getInstance()“; to reach the static instance but it won’t be “sam object” type will be “Singleton” “.”__CLASS__” gives the scope class not the real object class.

    First: you must read about “late static binding” in php and learn the limitations of self:: and __CLASS__ use “static::” instead of “self::” (5.3+)

    Or you can change all the pattern use statics like;

       <?php 
        class Singleton
        {
            // A static property to hold the single instance of the class
            private static $instance;
    
            // The constructor is private so that outside code cannot instantiate
            public function __construct() { }
    
            // All code that needs to get and instance of the class should call
            // this function like so: $db = Database::getInstance();
            public static function getInstance()
            {
                // If there is no instance, create one
                if (!isset(self::$instance)) {
                    $c = __CLASS__;
                    self::$instance = new $c;
                }
                return self::$instance;
            }
    
            // Block the clone method
            private function __clone() {}
    
    
            // Function for inserting data to object
            public function insertData($param, $element)
            {
                $this->{$param} = $element;
            }
        }
    
        //---CLASS ram---
        class ram
        {
            function __construct() 
            {
                $db = Singleton::getInstance();
                $db->insertData('name', 'Suresh');
            }
        }
    
        $obj_ram = new ram;
    
        //---CLASS sam---
        class sam extends Singleton
        {
            function __construct()
            {
                parent::__construct();
            }
    
            public static function show_data()
            {
                echo "<br>Data in current object<br>";
                print_r(self::getInstance());
                echo "<br><br>Data in singleton object<br>";
                $singleton_obj = Singleton::getInstance();
                print_r($singleton_obj);
            } 
        }
    
    $obj_sam = sam::getInstance();
    print_r($obj_sam);
    
        echo sam::show_data();
    

    This is an example which setting the properties pointers to the current object like “CI”

    <?php 
        class Singleton
        {
            // A static property to hold the single instance of the class
            private static $instance;
    
            // The constructor is private so that outside code cannot instantiate
            public function __construct() {
    
                if(isset(self::$instance))
                    foreach(self::$instance as $key => &$val)
                    {
                        $this->{$key} = &$val;
                }
            }
    
    
    
            // All code that needs to get and instance of the class should call
            // this function like so: $db = Database::getInstance();
            public static function getInstance()
            {
                // If there is no instance, create one
                if (!isset(self::$instance)) {
                    $c = __CLASS__;
                    self::$instance = new $c;
                }
                return self::$instance;
            }
    
            // Block the clone method
            private function __clone() {}
    
    
            // Function for inserting data to object
            public function insertData($param, $element)
            {
                $this->{$param} = $element;
            }
        }
    
        //---CLASS ram---
        class ram
        {
            function __construct() 
            {
                $db = Singleton::getInstance();
                $db->insertData('name', 'Suresh');
            }
        }
    
         class ram2
        {
            function __construct() 
            {
                $db = Singleton::getInstance();
                $db->insertData('name', 'Suresh');
                $db->insertData('name2', 'Suresh2');
            }
        }
    
        $obj_ram = new ram;
        $obj_ram = new ram2;
    
        //---CLASS sam---
        class sam extends Singleton
        {
            function __construct()
            {
                parent::__construct();
            }
    
            public function show_data()
            {
                echo "<br>Data in current object<br>";
                print_r($this);
    
                echo "<br><br>Data in singleton object<br>";
                $singleton_obj = Singleton::getInstance();
                print_r($singleton_obj);
            } 
        }
    
        $obj_sam = new sam;
        echo $obj_sam->show_data(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to write following query through codeigniter's db helper class, guide me plz
I was asked this question in an interview Plz tell me the answer :-
i m trying to implement MFMailViewComposeViewController... but getting some following error plz tell why
plz tell me whats wrong in code bcz i m not able to add
Any one plz tell me why i am getting this error ?? firstly my
I'm getting the following error while disabling publishing and distribution. Plz, tell me how
Do you know any elegant way to achieve this in ruby plz ? string
am getting this error when i open my site in internet explorer......... plz help
in the below code plz tell me how to load the text in textarea
I want to know about Microsoft Enterprise Library 5.0 . Can u plz tell

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.