0

我有3节课。[1]单例 [2]加载 [3]仪表板。在 Load 类中,有一种方法称为“model()”。我正在使用此代码初始化单例对象的数据。

$obj = Singleton::getInstance();
$obj->insertData('email', 'mail@domain.com');

同样,在 Dashboard 类中,有一个名为“show()”的方法,我正在尝试打印 Singleton 对象数据。但是,在这里我可以看到 Singleton 对象的所有数据,除了由“Load”类的“model”方法初始化的数据。

这是我的完整代码...

<?php 
//---Singletone Class---
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;
    }
}


//---LOAD class---
class Load
{    
    function __construct() 
    {
     $obj = Singleton::getInstance();
     $obj->insertData('country', 'INDIA');
    } 

    function model()
    {
        $this->name = 'Suresh';     

        $obj = Singleton::getInstance();
        $obj->insertData('email', 'mail@domain.com');
    }

    function msg()
    {
        return('<br><br>This message is from LOAD class');
    }       
}

$obj = Singleton::getInstance();
$load = new load();
$obj->load = $load;



//---Dashboard Class---
class Dashboard extends Singleton
{
    function __construct()
    {
        parent::__construct();
   }

    function show()
    {
        echo "Default data in current Object";
        echo "<br>";
        print_r($this);

        echo $this->load->msg();

        $this->load->model();

        echo "<br><br>Data in current Object after post intialization";
        echo "<br>";
        print_r($this);
    }
}

$dashboard = new dashboard();
$dashboard->show();
4

2 回答 2

0

我不喜欢你直接访问像数组这样的对象。这是一种更好的方法[见这里]

你应该这样称呼它:

$obj = Singleton::getInstance();
$load = new Load();
$obj->insertData( 'load', $load );

单例的实现:

class Singleton
{

// A static property to hold the single instance of the class
private static $instance;

// my local data
protected $_properties;

// You might want to move setter/getter to the end of the class file

public function __set( $name, $value )
{
    $this->_properties[ $name ] = $value;
}

public function __get( $name )
{
    if ( ! isset( $this->_properties[ $name ] )) {
       return null;
    }

    return $this->_properties[ $name ];
}

// No need to check, if single instance exists! 
// __construct can only be called, if an instance of Singleton actually exists
private function __construct() {

  $this->_properties = array();

  foreach(self::$instance as $key => &$val)
  {
     $this->_properties{$key} = &$val;
  }

}

public static function getInstance()
{
    if (!isset(self::$instance)) {
        $c = __CLASS__;
        self::$instance = new $c;
    }
    return self::$instance;
}

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


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

}

于 2012-07-22T14:49:18.227 回答
0

如果你的单身人士真的是单身人士,那么更新就会奏效。我怀疑您可能有多个已初始化的单例类实例。
编辑:
从真正的单例类继承也不是一个好主意。您需要删除 Dashboard 在 Singleton 上的继承

编辑: PHP 单例类的最佳实践

于 2012-07-22T14:07:33.363 回答