PHP code example of romanlazko / support-macroable

1. Go to this page and download the library: Download romanlazko/support-macroable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

romanlazko / support-macroable example snippets


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!

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

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!

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

ExampleClass::flushMacros();