php高级之框架源码、宏扩展原理与开发

作者 : admin 本文共3255个字,预计阅读时间需要9分钟 发布时间: 2024-06-10 共1人阅读

在使用框架的时候我们经常会看到如下代码
php高级之框架源码、宏扩展原理与开发插图
类的方法不会显示地声明在代码里面,而是通过扩展的形式后续加进去,这么做的好处是可以降低代码的耦合度、保证源码的完整性、团队开发的时候可以分别写自己的服务去扩展类,减少代码冲突等等。我自己看着框架源码实现了这个功能。
以下是结果:
php高级之框架源码、宏扩展原理与开发插图(1)
base代码
php高级之框架源码、宏扩展原理与开发插图(2)
index.php

<?php
require_once "macroable.php";
require_once "base.php";
$a = new phpmacro\Base();
$a::macro("first",function (){
    $this->query .="I had be macro";
    return $this->query;
});
$a->first();
var_dump($a->query);

base.php

<?php
namespace phpmacro;


/**
 * @method first()
 */
class Base 
{
	use Macroable;
    public $query = "init-query";
	function __construct()
	{
		// code...
		echo "base-contruct";
	}
}

Macroable.php

<?php
namespace phpmacro;
use BadMethodCallException;
use Closure;
use ReflectionClass;
use ReflectionMethod;
trait Macroable
{
/**
* The registered string macros.
*
* @var array
*/
protected static $macros = [];
/**
* Register a custom macro.
*
* @param  string  $name
* @param  object|callable  $macro
* @return void
*/
public static function macro($name, $macro)
{
static::$macros[$name] = $macro;
}
/**
* Mix another object into the class.
*
* @param  object  $mixin
* @param  bool  $replace
* @return void
*
* @throws \ReflectionException
*/
public static function mixin($mixin, $replace = true)
{
$methods = (new ReflectionClass($mixin))->getMethods(
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
);
foreach ($methods as $method) {
if ($replace || ! static::hasMacro($method->name)) {
static::macro($method->name, $method->invoke($mixin));
}
}
}
/**
* Checks if macro is registered.
*
* @param  string  $name
* @return bool
*/
public static function hasMacro($name)
{
return isset(static::$macros[$name]);
}
/**
* Flush the existing macros.
*
* @return void
*/
public static function flushMacros()
{
static::$macros = [];
}
/**
* Dynamically handle calls to the class.
*
* @param  string  $method
* @param  array  $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
public static function __callStatic($method, $parameters)
{
if (! static::hasMacro($method)) {
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
$macro = static::$macros[$method];
if ($macro instanceof Closure) {
$macro = $macro->bindTo(null, static::class);
}
return $macro(...$parameters);
}
/**
* Dynamically handle calls to the class.
*
* @param  string  $method
* @param  array  $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (! static::hasMacro($method)) {
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
$macro = static::$macros[$method];
if ($macro instanceof Closure) {
$macro = $macro->bindTo($this, static::class);
}
return $macro(...$parameters);
}
}

其实原理就是利用了静态调用的魔术方法与匿名函数的bindTo方法实现了类与方法解藕的效果
以下是test.php,可以清晰的看明白匿名函数bindTo跟bind是怎么用的

<?php
/** 
* 复制一个闭包,绑定指定的$this对象和类作用域。 
* 
* @author 疯狂老司机 
*/
class Animal {
private static $cat = "cat";
private $dog = "dog";
public $pig = "pig";
}
/* 
* 获取Animal类静态私有成员属性
*/
$cat = static function() {
return Animal::$cat;
};
/* 
* 获取Animal实例私有成员属性
*/
$dog = function() {
return $this->dog;
};
/* 
* 获取Animal实例公有成员属性
*/
$pig = function() {
return $this->pig;
};
$bindCat = Closure::bind($cat, null, new Animal());// 给闭包绑定了Animal实例的作用域,但未给闭包绑定$this对象
$bindDog = Closure::bind($dog, new Animal(), 'Animal');// 给闭包绑定了Animal类的作用域,同时将Animal实例对象作为$this对象绑定给闭包
$bindPig = Closure::bind($pig, new Animal());// 将Animal实例对象作为$this对象绑定给闭包,保留闭包原有作用域
$animal = new Animal();
$pigBindto = $pig->bindTo($animal);
echo $pigBindto()."\r
";
echo $bindCat()."\r
";// 根据绑定规则,允许闭包通过作用域限定操作符获取Animal类静态私有成员属性
echo $bindDog()."\r
";// 根据绑定规则,允许闭包通过绑定的$this对象(Animal实例对象)获取Animal实例私有成员属性
echo $bindPig()."\r
";// 根据绑定规则,允许闭包通过绑定的$this对象获取Animal实例公有成员属性

输出结果
php高级之框架源码、宏扩展原理与开发插图(3)

本站无任何商业行为
个人在线分享 » php高级之框架源码、宏扩展原理与开发
E-->