dsdqpdjpq16640651 2012-07-08 09:57
浏览 51
已采纳

如何在扩展类中访问单例类对象

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(); 
?>
  • 写回答

3条回答 默认 最新

  • dovgqc2648 2012-07-08 10:28
    关注

    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(); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题