<?php
interface IUser
{
function getName();
}
class User implements IUser{
public function getName()
{
return make::instance()->install();
}
}
class User2 implements IUser{
public function getName()
{
return clea::instance()->del();
}
}
class User3 implements IUser{
public function getName()
{
$obj = new update();
return $obj->update2();
}
}
class UserFactory
{
public static function Create($class_name)
{
return new $class_name();
}
}
class make
{
use Instance;
public function install(){
return '创建';
}
}
class clea
{
use Instance;
public function del()
{
return '删除';
}
}
class update
{
use Instance;
public function update2()
{
$val = 1;
switch (true){
case $val < 1:
return 'yes_update';break;
case $val > 1:
return 'no_update';break;
default:
return 'update';
}
}
}
trait Instance
{
static private $_instance;
public static function instance($params = [])
{
if (!self::$_instance instanceof static) {
self::$_instance = new static(...$params);
}
return self::$_instance;
}
}
$uo = UserFactory::Create('user');
echo( $uo->getName()."\n" );
$user2 = UserFactory::Create('user2');
echo( $user2->getName()."\n" );
$user3 = UserFactory::Create('user3');
echo( $user3->getName()."\n" );