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

在使用框架的时候我们经常会看到如下代码
在这里插入图片描述
类的方法不会显示地声明在代码里面,而是通过扩展的形式后续加进去,这么做的好处是可以降低代码的耦合度、保证源码的完整性。我自己看着框架源码实现了这个功能。
以下是结果:
在这里插入图片描述
base代码
在这里插入图片描述
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\n";
echo $bindCat()."\r\n";// 根据绑定规则,允许闭包通过作用域限定操作符获取Animal类静态私有成员属性
echo $bindDog()."\r\n";// 根据绑定规则,允许闭包通过绑定的$this对象(Animal实例对象)获取Animal实例私有成员属性
echo $bindPig()."\r\n";// 根据绑定规则,允许闭包通过绑定的$this对象获取Animal实例公有成员属性

输出结果
在这里插入图片描述

相关推荐

  1. ubuntu编译安装memcached和php-memcache 扩展

    2024-06-07 11:46:02       48 阅读
  2. linux下php的psr.so扩展安装

    2024-06-07 11:46:02       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 11:46:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 11:46:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 11:46:02       87 阅读
  4. Python语言-面向对象

    2024-06-07 11:46:02       96 阅读

热门阅读

  1. 探索Web前端三大主流框架:React,Angular和Vue.js

    2024-06-07 11:46:02       28 阅读
  2. MongoDB 分布式 概述

    2024-06-07 11:46:02       28 阅读
  3. CSS中的长度单位详解

    2024-06-07 11:46:02       35 阅读
  4. 【面试题】Node.js高频面试题

    2024-06-07 11:46:02       24 阅读
  5. 开发常用的组件库

    2024-06-07 11:46:02       36 阅读
  6. Oracle 误删数据后回滚

    2024-06-07 11:46:02       26 阅读
  7. 正则表达式基础

    2024-06-07 11:46:02       32 阅读