美文网首页
php中使用macro宏

php中使用macro宏

作者: 耍帅oldboy | 来源:发表于2023-04-21 14:29 被阅读0次

PHP Macro

The PHP Macro package offers closure (anonymous function) based setter dependency injection by providing a trait which can be included into any class.

composer req aimeos/macro

This package is for application, framework and library developers who want to allow customizing the behavior of their code by their users.

Why macros

In applications, frameworks or libraries which are build for customization it’s necessary to allow overwriting existing functionality to be able customize its behavior. This is where macros are very handy because they can add custom code using closures.

With the PHP Macro package, you can also allow users to overwrite methods in base classes without forcing your users to extend these classes. The PHP Macro package uses NO reflection or other hacks, just pure PHP methods.

There are some pros and cons when compared to class based depencency injection:

Pro:

  • Less code to write and much easier to implement for simple stuff
  • Custom closures can be inherited and overwritten like class methods

Con:

  • Limited static code analysis possibilities
  • Anonymous function can not be forced to implement an interface

Thus, it's not a replacement for class based depencency injection but a lightweight addition for small extension points where full-blown dependency injection using classes implementing interfaces are too much work.

Allow customization

The result of existing methods can be modified if the original method checks for an existing macro and use that instead its own implementation:

// original code

class Order
{
    use Aimeos\Macro\Macroable;

    private $id = '123';

    public function getOrderNumber()
    {
        $fcn = static::macro( 'orderNumber' );
        return $fcn ? $fcn( $this->id ) : $this->id;
    }
};

Now, you can add your custom orderNumber macro that will be used instead:

// user code

Order::macro( 'orderNumber', function( string $id ) {
   return date( 'Y' ) . '-' . $id;
} );

(new Order)->getOrderNumber(); // now returns '2020-123'

Thus, you can generate own output or pass a different result to subseqent methods within the application.

Access class properties

When macros are called in an object context, they can also access class properties:

// original code

class A
{
    use Aimeos\Macro\Macroable;
    private $name = 'A';
};

Here, the private property $name is available in the macro:

// user code

A::macro( 'concat', function( array $values ) {
   return $this->name . ':' . implode( '-', $values );
} );

(new A)->concat( ['1', '2', '3'] ); // returns 'A:1-2-3'

The macro can use the property as input for creating the returned value.

Use inherited macros

The PHP macro package also allows to inherit macros from parent classes. Then, they can access class properties of the child class just like regular class methods:

// original code

class A
{
    use Aimeos\Macro\Macroable;
    private $name = 'A';
};

class B extends A
{
    private $name = 'B';
};

Macros added to the parent class will be available in child classes too:

// user code

A::macro( 'concat', function( array $values ) {
   return $this->name . ':' . implode( '-', $values );
} );

(new B)->concat( ['1', '2', '3'] ); // returns 'B:1-2-3'

Class B extends from class A but provides a different $name property. The macro inherited from class A will now use the property of class B.

Overwrite inherited macros

It's also possible to overwrite macros inherited from parent classes as it's possible with regular class methods:

// original code

class A
{
    use Aimeos\Macro\Macroable;

    public function do() {
        return static::macro( 'concat' )( [1, 2, 3] );
    }
};

class B extends A {};

class C extends A {};

Now you can add macros to the parent class and one of the child classes:

// user code

A::macro( 'concat', function( array $values ) {
   return implode( ',', $values );
} );

C::macro( 'concat', function( array $values ) {
   return implode( '-', $values );
} );

(new B)->do(); // returns '1,2,3'

(new C)->do(); // returns '1-2-3'

This enables you to add special handling for single classes even if all other classes still use the macro added to class A.

Overwrite protected methods

Base classes often offer a set of methods that are used by the child classes. In PHP, replacing the methods of a base class is impossible and thus, you have to overwrite each child class with your own implementation.

To avoid that, the original method can use the call() method instead of calling the method of the parent class directly:

// original code

class A
{
    use Aimeos\Macro\Macroable;

    protected function getName( $prefix )
    {
        return $prefix . 'A';
    }
};

class B extends A
{
    public function do()
    {
        return $this->call( 'getName', 'B-' );
    }
};

This will check if there's a macro getName available and will call that instead of the getName() method:

// user code

(new B)->do(); // returns 'B-A'

A::macro( 'getName', function( $prefix ) {
   return $this->getName( $prefix ) . '-123';
} );

(new B)->do(); // returns 'B-A-123'

The original getName() method can still be used in the macro.

Reset macros

Sometimes, it may be necessary to remove macros from objects, especially when running automated tests. You can unset a macro by using:

class A
{
    use Aimeos\Macro\Macroable;
};

// add macro
A::macro( 'test', function() {
   return 'test';
} );

// remove macro
A::unmacro( 'test' );

相关文章

  • 宏(Macro)指的是 Rust 中一系列的功能 声明(Declarative)宏, 使用macro_rules!...

  • 重写 NSLog

    重写NSLog 使用上述宏,需要build settings的macro中DEBUG设置正确。 常用输出:

  • freemarker(三) 宏

    what 宏是在模板中使用macro指令定义 宏是和某个变量关联的模板片断,以便在模板中通过用户定义指令使用该变量...

  • C/C++ 中的宏/Macro

    宏(Macro)本质上就是代码片段,通过别名来使用。在编译前的预处理中,宏会被替换为真实所指代的代码片段,即下图中...

  • 模板宏

    声明宏 {% macro 宏的名字(参数) %}​内容{% endmacro %} 调用宏 {{ 宏的名字(参数...

  • 数据分析师-Week16

    1.调用rar解压.rar 2.在宏(macro)中高亮显示关键字 3.用宏macro处理参数并返回结果

  • JS事件循环机制之宏任务/微任务

    宏任务(macro-task)macro-task.png 微任务(micro-task)micro-task.p...

  • zabbix自动发现规则的使用-网络设备实践

    在Zabbix监控实际应用中,有一种自动发现(LLD)函数中使用的宏,其类型为:{#MACRO}它是一个在LLD规...

  • 宏指令(Macro)

    人类宇宙,克莱因瓶 衡宇宙,克莱因瓶 量子计算机,瓶塞 瓶外,无限 无限侵入有限时空, 所以我们将历经无数种可能 ...

  • 关于rust宏补充(二)-派生过程宏示例

    rust中过程宏示例: 准备工作 定义一个过程宏: proc_macro_derive 测试 更多例子[https...

网友评论

      本文标题:php中使用macro宏

      本文链接:https://www.haomeiwen.com/subject/facjjdtx.html