PHP OOP单例不';t返回对象
奇怪的麻烦。我已经多次使用singleton,但是这个特殊的案例不起作用。Dump表示该实例为nullPHP OOP单例不';t返回对象,php,oop,singleton,Php,Oop,Singleton,奇怪的麻烦。我已经多次使用singleton,但是这个特殊的案例不起作用。Dump表示该实例为null define('ROOT', "/"); define('INC', 'includes/'); define('CLS', 'classes/'); require_once(CLS.'Core/Core.class.php'); $core = Core::getInstance(); var_dump($core->instance); $core->settings
define('ROOT', "/");
define('INC', 'includes/');
define('CLS', 'classes/');
require_once(CLS.'Core/Core.class.php');
$core = Core::getInstance();
var_dump($core->instance);
$core->settings(INC.'config.php');
$core->go();
核心类
class Core
{
static $instance;
public $db;
public $created = false;
private function __construct()
{
$this->created = true;
}
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
} else {
return self::$instance;
}
}
public function settings($path = null)
{
...
}
public function go()
{
...
}
}
错误代码
Fatal error: Call to a member function settings() on a non-object in path
这可能是一些愚蠢的打字错误,但我的编辑器中没有任何错误。感谢您一如既往的快速回复 您需要始终从singleton方法返回singleton对象,因为您有一个
else
语句,所以第一次调用getInstance
不会返回任何内容:
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
} else {
return self::$instance;
}
}
您的单例方法应如下所示:
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
另外,拥有一个实例变量来指示是否创建了一个对象是非常无用的,因为您可以比较
if(self::$instance!==NULL)
,然后就可以开始了。您需要始终从singleton方法返回singleton对象,这不是因为您有一个else
语句,因此,第一次调用getInstance
不会返回任何内容:
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
} else {
return self::$instance;
}
}
您的单例方法应如下所示:
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
另外,拥有一个实例变量来指示是否创建了一个对象是非常无用的,因为您只需比较if(self::$instance!==NULL)
就可以了。getInstance应该总是返回一个值--需要这样更改:
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
getInstance应始终返回一个值--需要如下更改:
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
static function getInstance()
{
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
除了需要将getInstance()方法更改为:
static function getInstance() {
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
…您还在以下调用中尝试从实例本身取消引用$instance:
var_dump($core->instance);
您应该检查:
var_dump($core);
或
…在$core=core::getInstance()调用之后,它应该是相同的对象。除了需要将getInstance()方法更改为:
static function getInstance() {
if(!self::$instance) {
self::$instance = new Core();
}
return self::$instance;
}
…您还在以下调用中尝试从实例本身取消引用$instance:
var_dump($core->instance);
您应该检查:
var_dump($core);
或
…在$core=core::getInstance()调用之后,它应该是同一个对象。+1另外,我认为
$this->created
位(在OP中)是没有用的,因为从技术上讲(self::$instance!==null)
和$this->created==true
是等价的。我现在知道了,但我没有意识到我有这个错误。同样感谢+1,我认为$this->created
位(在OP中)有点无用,因为从技术上讲(self::$instance!==null)
和$this->created==true
是等价的。我现在知道了-但我没有意识到我有这个错误。谢谢