Coder Social home page Coder Social logo

macroable's Introduction

安装

composer require ledc/macroable

使用

<?php

use Ledc\Macroable\Macro;
use Ledc\Macroable\Macroable;

require_once __DIR__ . '/vendor/autoload.php';
class Tests
{
    //use Macroable;
    use Macro;
}

class Request
{
    //use Macroable;
    use Macro;
}

$ts = new Tests();
$ts->macro('hello', function () {
    echo 'hello Tests' . PHP_EOL;
});
$ts->hello();

$req = new Request();
$req->macro('hello', function () {
    echo 'hello Request' . PHP_EOL;
});
$req->hello();

Usage

You can add a new method to a class using macro:

$macroableClass = new class() {
    use Ledc\Macroable\Macroable;
};

$macroableClass::macro('concatenate', function(... $strings) {
   return implode('-', $strings);
});

$macroableClass->concatenate('one', 'two', 'three'); // returns 'one-two-three'

Callables passed to the macro function will be bound to the class

$macroableClass = new class() {
    protected $name = 'myName';
    use Ledc\Macroable\Macroable;
};

$macroableClass::macro('getName', function() {
   return $this->name;
};

$macroableClass->getName(); // returns 'myName'

You can also add multiple methods in one go by using a mixin class. A mixin class contains methods that return callables. Each method from the mixin will be registered on the macroable class.

$mixin = new class() {
    public function mixinMethod()
    {
       return function() {
          return 'mixinMethod';
       };
    }
    
    public function anotherMixinMethod()
    {
       return function() {
          return 'anotherMixinMethod';
       };
    }
};

$macroableClass->mixin($mixin);

$macroableClass->mixinMethod() // returns 'mixinMethod';

$macroableClass->anotherMixinMethod() // returns 'anotherMixinMethod';

最佳实践

  • 宏指令方法不存在状态:用Ledc\Macroable\Macroable
  • 宏指令方法存在状态或依赖上下文时,用:Ledc\Macroable\Macro

使用场景

特性trait 使用场景
Ledc\Macroable\Macroable PHP-FPM,或宏指令方法无状态
Ledc\Macroable\Macro PHP-CLI,中间件内对请求对象注入方法(请求结束销毁)

macroable's People

Contributors

ledccn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.