Php 如何在扩展类中访问单例类对象
请告诉我哪里做错了 我有三节课。这些是这样的Php 如何在扩展类中访问单例类对象,php,object,singleton,this,Php,Object,Singleton,This,请告诉我哪里做错了 我有三节课。这些是这样的 Singleton类,其中我遵循Singleton设计模式 类ram 山姆班 在“ram”类中,我正在为单例类对象设置数据 现在,在“山姆”课上。。我试图访问sam类的show_data()函数中的singleton类对象 当,我正在使用 Print_r($this) : showing empty object 但是,当我使用以下代码时 $singleton_obj = Singleton::getInstance(); print_r($sin
Print_r($this) : showing empty object
但是,当我使用以下代码时
$singleton_obj = Singleton::getInstance();
print_r($singleton_obj); : Showing content of singleton object
我的问题是,
为什么在打印($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();
?>
您的单例没有得到“初始化”-它没有创建任何对象。所以,
$这个
什么都不是
编辑:结果我错了-我通常看到单例将_construct()声明为private
。这就是拒绝实例化类的新实例的方式。但我不确定你的情况
编辑2:问题出在哪里?您的代码的行为符合预期。您正在创建一个新的sam
,其中没有数据,因此您得到的是一个空对象。打印singleton对象时,可以看到singleton的数据
编辑3:我从未见过单身汉被扩展。我认为这是该模式的错误应用,我当然可能是错误的。您正在通过“新建sam
”创建一个“sam
”对象,您应该使用“sam::getInstance()
”;要访问静态实例,但它不会是“sam对象”,类型将是“Singleton”。“\uuuuuu CLASS\uuuuu”给出的范围类不是真正的对象类
首先:您必须阅读php中的“”,了解self::的限制,并使用“静态:
”而不是“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();
您无法从该类对象中访问该类的静态属性
声明为静态的属性不能通过实例化的类对象访问(尽管静态方法可以)。
如果你的insertData函数中有一个额外的},至少在这里的代码中不确定这是否只是一个输入错误,或者它是否存在于你的代码中。为什么getInstance不是一个静态方法?你的评论是u construct()private
但是你的代码声明它是公共的吗?@Laxus它需要是静态的吗?我对PHP没有深入的OOP知识。顺便说一下,即使我将其更改为静态,也不会产生任何效果。你的代码按照预期运行。你不应该尝试扩展单例。这不是使用模式的方式。你只需要一个s实例ingleton-无克隆或子项。@adiya menon通过此代码,我正在初始化singleton../---CLASS ram---CLASS ram{function{uuuu construct(){$db=singleton::getInstance();$db->insertData('name','Suresh');}$obj_ram=new ram;嘿,我想要$this指针中的singleton类对象…我可以得到singleton对象的所有数据。你需要仔细阅读这件事,我的朋友,这是一个错误的方法。嘿,你看过PHP的codeigniter框架吗。他们使用相同的技术。他们有一个主要的singleton控制器类&他们在另一个控制器类中扩展了该类。我只是试图模拟这种技术,但是..不知道我在哪里做错了。是的,刚刚去看了CI的代码,我也不明白那里发生了什么,看起来像是一个我将从中学到一些东西的问题:)祝你好运!嘿,非常感谢你通过显示准确的代码来帮助我。这真的很有帮助。你能告诉我,在show_data()中有什么机制吗我可以通过使用$this来获取singleton对象的所有数据。例如:print\r($this)://应该提供所有singleton对象数据如果你想使用“$this”,你必须将你的代码写入singleton类,因为“$instance”的类型是“singleton”。你为什么不使用“singleton::GetInstance()“如果你想要单例对象属性?嘿,我不太擅长PHP OOP。你能帮我看一些代码吗。我是说,什么。。正是我需要在singleton类中写作。因此,在“sam”类的show_data()中,我可以通过$this获得这些数据。嘿,我想你对codeigniter很熟悉。我只想和你一样。通过使用$this,我需要从任何控制器类访问singleton对象的所有数据。阅读Core-controller.php的代码,它将获得静态实例并在构造函数方法(该死!)之后重建对象,现在,我将重新编写您的代码等待。
<?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();