Plz告诉我哪里做错了。。。
我有三节课。这些是这样的。。
- Singleton类,我遵循Singleton设计模式
- Class ram
- sam类
在"ram"类中,我为singleton类对象设置数据。
现在,在"sam"课上。。我正在尝试访问sam类的showdata()函数中的singleton类对象。
何时,我使用。。
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)来获取singleton类对象的内容。
我的类文件是
<?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();
?>
您正在通过"new sam
"创建"sam
"对象,您必须使用"sam::getInstance()
";要到达静态实例,但它不会是"sam对象"类型将是"Singleton"。"__CLASS__"给出的是作用域类,而不是真正的对象类。
首先:您必须阅读php中的"后期静态绑定",并了解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();
您的singleton没有被"初始化"-它没有创建任何对象。所以$this
没有任何意义。
编辑:原来我错了——我经常看到辛格尔顿将__construct()声明为private
。这就是类的新实例被拒绝实例化的原因。但不确定你的情况如何。
编辑2:问题出在哪里?您的代码的行为符合预期。您正在创建一个新的sam
,其中没有任何数据,所以您得到的是一个空对象。当您打印singleton对象时,您看到的是singleton的数据。
第三版:我从未见过辛格尔顿被延长。我认为这是模式的错误应用,当然我可能错了。
您不能从类对象中访问类的静态属性。
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