1. 单例模式
- 拥有一个构造函数, 并且为private
- 拥有一个静态成员变量用来保持类的实例
- 拥有一个访问这个实例的静态方法
namespace script;
class Test
{
private static $instance = null;
public static function getInstance(){
if (is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
private function __construct()
{
}
private function __clone()
{
}
}
2. 注册树模式
- 注册树模式通过将对象实例注册到一颗全局的对象树上
- 需要的时候从对象树上采摘下来使用
namespace script;
class TreeRegister
{
protected static $objects = null;
public static function set($key, $object){
self::$objects[$key] = $object;
}
public static function get($key){
if (!isset(self::$objects[$key])){
self::$objects[$key] = new $key;
}
return self::$objects[$key];
}
public static function _unset($key){
unset(self::$objects[$key]);
}
}
namespace script;
class A{
public function abc(){
return 'abc';
}
}
$a = new A();
TreeRegister::set('A', $a);
TreeRegister::get('A')->abc();
3. 依赖注入
- 依赖注入主要用来减少代码间的耦合
- 有效分离对象和它所需要的外部资源
namespace script;
class Person
{
public static function buy($obj){
return $obj->pay();
}
}
namespace script;
class Car{
public function pay(){
return 1;
}
}
namespace script;
class House{
public function pay(){
return 2;
}
}
$car = new Car();
$house = new House();
Person::buy($car);
Person::buy($house);
4. 反射机制
- 利用反射机制可以实例化类以及使用和访问类方法, 属性, 参数和注释等
namespace script;
class A
{
public $a = 1;
private $b = 2;
public static $instance = null;
public function ab($a, $b){
return 'a: '.$a .'_b: ' . $b;
}
public function cd($type = 1){
return 'cd: '. $type;
}
}
$a = new A();
$obj = new \ReflectionClass($a);
$instance = $obj->newInstance();
$methods = $obj->getMethods();
foreach ($methods as $method){
$method->getDocComment();
}
$obj->getProperties();
$instance->ab('123', '456');
$method = $obj->getMethod('ab');
$method->invokeArgs($instance, ['111', '222']);
$method = $obj->getMethod('cd');
$method->invoke($instance);
$objMethod = new \ReflectionMethod($a, 'ab');
$objMethod->isPublic();
$objMethod->isStatic();
$objMethod->getParameters();
$objMethod->getNumberOfParameters();
5. 工厂模式
namespace script;
interface Sms
{
public static function send($phone, $code);
}
namespace script;
class Ali implements Sms
{
public static function send($phone, $code){
return 'Ali-Sms';
}
}
namespace script;
class Qiniu implements Sms
{
public static function send($phone, $code)
{
return 'Qiniu-Sms';
}
}
namespace script;
class Baidu implements Sms
{
public static function send($phone, $code)
{
return 'Baidu-Sms';
}
}
include 'Sms.php';
$type = 'Ali';
include $type.'.php';
$class = "script\\".$type;
echo $class::send($phone,$code);
6. 容器
namespace script;
class Container
{
protected static $instance = null;
public $objects = [];
private function __construct()
{
}
public static function getInstance(){
if (is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
public function set($key, $value){
$this->objects[$key] = $value;
}
public function get($key){
if ($this->objects[$key]) $key = $this->objects[$key];
$reflect = new \ReflectionClass($key);
$construct = $reflect->getConstructor();
if (!$construct){
return new $key;
}
$params = $construct->getParameters();
if (!$params) return new $key;
$args = [];
foreach ($params as $param){
$class = $param->getClass();
if ($class) {
$args[] = $this->get($class->name);
}
}
return $reflect->newInstanceArgs($args);
}
}
Container::getInstance()->set('person', 'script\Person');
Container::getInstance()->set('car', 'script\Car');
$obj = Container::getInstance()->get('person');
var_dump($obj->buy());
namespace script;
class Person
{
public $obj;
public $name;
public function __construct(Car $object, $name = 'nobody')
{
$this->obj = $object;
$this->name = $name;
}
public function buy(){
return $this->name . '|' . $this->obj->pay();
}
}
namespace script;
class Car{
public function pay(){
return 1;
}
}