<?php
class LoveBase
{
protected static $_instance = NULL;
protected function __construct() {}
public static function app()
{
if(self::$_instance == NULL) {
self::$_instance = new self();
}
return self::$_instance;
}
public function get()
{
return 'LoveBase';
}
}
class Love extends LoveBase
{
public static function app()
{
if(self::$_instance == NULL) {
self::$_instance = new self();
}
return self::$_instance;
}
public function get()
{
return 'Love';
}
}
// Print "LoveLove" in this case(first case)
echo Love::app()->get();
echo LoveBase::app()->get();
// Print "LoveBaseLoveBase" in this case(second case)
// echo LoveBase::app()->get();
// echo Love::app()->get();
为什么两种不同的方法得出相同的结果?
比较这两种情况,当它的类首先实例化时,该方法将起作用。
(对不起,我英语不好,希望你能说得通)