All Projects → spatie → Macroable

spatie / Macroable

Licence: mit
A trait to dynamically add methods to a class

Programming Languages

macros
77 projects

Projects that are alternatives of or similar to Macroable

Objc4
A latest buildable and debuggable Objective-C runtime (objc4-818.2) project.
Stars: ✭ 331 (-20.24%)
Mutual labels:  runtime
Mac Mouse Fix
Mac Mouse Fix - A simple way to make your mouse better.
Stars: ✭ 362 (-12.77%)
Mutual labels:  utility
Golang runtime reading
golang 1.10.2 runtime code reading - golang runtime源码分析。只有思考过,你才会印象深刻。
Stars: ✭ 393 (-5.3%)
Mutual labels:  runtime
Mbpmid2010 gpufix
MBPMid2010_GPUFix is an utility program that allows to fix MacBook Pro (15-inch, Mid 2010) intermittent black screen or loss of video. The algorithm is based on a solution provided by user fabioroberto on MacRumors forums.
Stars: ✭ 334 (-19.52%)
Mutual labels:  utility
Anymethodlog
Log any method call of object in Objective-C
Stars: ✭ 361 (-13.01%)
Mutual labels:  runtime
Ttpatch
热修复、热更新、JS代码动态下发、动态创建类
Stars: ✭ 372 (-10.36%)
Mutual labels:  runtime
Haxor News
Browse Hacker News like a haxor: A Hacker News command line interface (CLI).
Stars: ✭ 3,342 (+705.3%)
Mutual labels:  utility
Android Runtime
Android runtime for NativeScript (based on V8)
Stars: ✭ 399 (-3.86%)
Mutual labels:  runtime
Vuex Rest Api
A utility to simplify the use of REST APIs with Vuex
Stars: ✭ 365 (-12.05%)
Mutual labels:  utility
Uwebsockets.js
μWebSockets for Node.js back-ends 🤘
Stars: ✭ 4,552 (+996.87%)
Mutual labels:  runtime
Ms
Tiny millisecond conversion utility
Stars: ✭ 3,762 (+806.51%)
Mutual labels:  utility
Ts Runtime
Runtime Type Checks for TypeScript
Stars: ✭ 354 (-14.7%)
Mutual labels:  runtime
Version Checker
Kubernetes utility for exposing image versions in use, compared to latest available upstream, as metrics.
Stars: ✭ 371 (-10.6%)
Mutual labels:  utility
Underscore.string
String manipulation helpers for javascript
Stars: ✭ 3,355 (+708.43%)
Mutual labels:  utility
Monolingual
Remove unnecessary language resources from macOS.
Stars: ✭ 393 (-5.3%)
Mutual labels:  utility
Gtk For Windows Runtime Environment Installer
GTK+ for Windows Runtime Environment Installer (fork from http://gtk-win.sourceforge.net)
Stars: ✭ 325 (-21.69%)
Mutual labels:  runtime
Jql
A JSON Query Language CLI tool
Stars: ✭ 368 (-11.33%)
Mutual labels:  utility
Lodash Php
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP
Stars: ✭ 412 (-0.72%)
Mutual labels:  utility
Common.utility
Various helper class
Stars: ✭ 4,101 (+888.19%)
Mutual labels:  utility
Gdx Texture Packer Gui
A simple way to pack and manage texture atlases for LibGDX game framework.
Stars: ✭ 371 (-10.6%)
Mutual labels:  utility

A trait to dynamically add methods to a class

Latest Version on Packagist run-tests Total Downloads

This package provides a trait that, when applied to a class, makes it possible to add methods to that class at runtime.

Here's a quick example:

$myClass = new class() {
    use Spatie\Macroable\Macroable;
};

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

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

The idea of a macroable trait and the implementation is taken from the macroable trait of the Laravel framework.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/macroable

Usage

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

$macroableClass = new class() {
    use Spatie\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 Spatie\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';

Changelog

Please see CHANGELOG for more information on what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Idea and code is taken from the macroable trait of the Laravel framework.

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].