dsdqpdjpq16640651 2012-07-08 01: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 02: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 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
  • ¥15 Linux环境下openssl报错
  • ¥15 我在使用VS编译并执行之后,但是exe程序会报“无法定位程序输入点_kmpc_end_masked于动态链接库exe上“,请问这个问题有什么解决办法吗
  • ¥15 el-select光标位置问题
  • ¥15 单片机 TC277 PWM
  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部