承接 romanlazko/support-macroable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

romanlazko/support-macroable

最新稳定版本:1.1

Composer 安装命令:

composer require romanlazko/support-macroable

包简介

Trait Macroable to dynamically add methods to your PHP classes.

README 文档

README

A PHP trait that enables dynamic method addition to classes at runtime, inspired by Laravel's Macroable trait.

Installation

You can install the package via Composer:

composer require romanlazko/support-macroable

Usage

Basic Macro Registration

use RomanLazko\Support\Traits\Macroable;

class ExampleClass
{
    use Macroable;
}

// Register a macro
ExampleClass::macro('greet', function ($name) {
    return "Hello, {$name}!";
});

// Use the macro
echo ExampleClass::greet('World'); // Output: Hello, World!

Instance Macros

Macros can also be called on class instances:

$example = new ExampleClass();
echo $example->greet('World'); // Output: Hello, World!

Mixin Support

You can mix in multiple methods from another class:

class MyMacros {
    public function greet($name) {
        return "Hello, {$name}!";
    }
    
    public function farewell($name) {
        return "Goodbye, {$name}!";
    }
}

ExampleClass::mixin(new MyMacros);

echo ExampleClass::greet('World'); // Output: Hello, World!
echo ExampleClass::farewell('World'); // Output: Goodbye, World!

Checking for Macros

Check if a macro exists:

if (ExampleClass::hasMacro('greet')) {
    // Macro exists
}

Flushing Macros

Remove all registered macros:

ExampleClass::flushMacros();

Available Methods

  • macro(string $name, callable $macro): Register a new macro
  • mixin(object $mixin, bool $replace = true): Mix in methods from another class
  • hasMacro(string $name): bool: Check if a macro exists
  • flushMacros(): Remove all registered macros

Requirements

  • PHP 7.4 or higher

License

This package is open-sourced software licensed under the MIT license.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-24