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

时间:2012-07-08 09:57:49

标签: php object singleton this

Plz告诉我我在哪里做错了......

我有3节课。这些都是这样的..

  1. 我遵循单身设计模式的单身人士课程
  2. Class ram
  3. 班级山姆
  4. 在'ram'类我正在设置单例类对象的数据。

    现在,在'sam'类中..我正在尝试访问sam类的show_data()函数中的单例类对象。

    什么时候,我正在使用..

    Print_r($this) : showing empty object
    

    但是,当我使用以下代码时..

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

    我的问题是, 为什么在 Print_r($ this)的情况下,它显示空对象。有没有办法,我可以使用 Print_r($ this)获取单件类对象的内容。

    MY类文件就是这个..

    <?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 个答案:

答案 0 :(得分:2)

您正在使用“sam”创建“new sam”对象,您可以使用“sam::getInstance()”;到达静态实例但它不会是“sam对象”类型将是“Singleton”“。” __ CLASS __ “给范围类不是真实对象类。

首先:您必须在php中阅读“late static binding”并了解self ::和 __ CLASS __ 使用“static::”代替“self::”(5.3 +)

或者您可以更改所有模式使用静态,如;

   <?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();

这是一个将属性指针设置为当前对象的示例,如“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(); 

答案 1 :(得分:0)

你的单身人士没有被初始化&#39; - 它不会创建任何对象。所以$this指的是什么。

编辑:原来我错了 - 我经常看到Singletons将__construct()声明为private。这就是如何拒绝实例化该类的新实例。不知道你的情况发生了什么。

编辑2:问题是什么?您的代码按预期运行。您正在创建一个没有数据的新sam,因此您将获得一个空对象。当你打印单身对象时,你会看到单身人士的数据。

编辑3:我从未见过单身人士被延长。我认为这种模式的错误应用,当然我可能是错的。

答案 2 :(得分:0)

您无法从该类对象中访问类的静态属性。

A property declared as static can not be accessed with an instantiated class object (though a static method can).

http://php.net/manual/en/language.oop5.static.php