这个单身人士课有什么不对

时间:2012-07-22 13:56:22

标签: php singleton

我有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();

2 个答案:

答案 0 :(得分:0)

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

编辑: Best practice on PHP singleton classes

答案 1 :(得分:0)

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

您应该这样称呼它:

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

Singleton的实施:

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() {}

}