单例模式:一个类仅允许被实例化一次

Original 2019-06-04 15:28:10 183
abstract:<?php class Hubby{private function __construct() {}private function __clone() {}protected static $instance = null; public static function getInstance() { if (is_null(static::$instance)) { s

<?php 

class Hubby

{

private function __construct() {}

private function __clone() {}

protected static $instance = null;

public static function getInstance()

{

if (is_null(static::$instance)) {

static::$instance = new static();

}


return static::$instance;

}

}



$hubby1 = Hubby::getInstance();

$hubby2 = Hubby::getInstance();


echo ($hubby1 instanceof Hubby) ? '是' : '不是';

echo '<br>';

echo ($hubby2 instanceof Hubby) ? '是' : '不是';

echo '<br>';

echo ($hubby1 === $hubby2) ? '完全相等' : '不相等';

echo '<br>';

var_dump($hubby1, $hubby2);



Correcting teacher:查无此人Correction time:2019-06-05 09:19:27
Teacher's summary:完成的不错,学习完类,就相当于php入门了。继续加油。

Release Notes

Popular Entries