laravel-Macroable 详解
作者:钓赛通
发布时间:2023-04-14
点击数:
laravel-Macroable
说明:类型为trait,引入后可以在不修改类代码的情况下,为类植入新的方法(类的方法或静态方法均可)。使用__call实现
引入:
<?php
require 'vendor/autoload.php';
use Illuminate\Support\Traits\Macroable;
//$app = require_once 'bootstrap/app.php';
class test {
use Macroable;
public $test = 'param test';
public static $test2 = 'static param test2';
}
$o = new test;例子:
(1)植入类方法
$o::macro('run', function () {
echo $this->test;
});
$o->run();