novia713
11/19/2015 - 8:39 AM

Decorator Pattern replacement in functional PHP

Decorator Pattern replacement in functional PHP

<?php

$say_hello = function($name) {
    return "hello, $name!\n";
};

$log_say = function ($func)  {
    return function ($name) use ($func){
        echo "[ greeting mode enabled ]\n";
        echo $func($name);
    };
};

$logging_hello = function($name) use ($log_say, $say_hello) {
    $l = $log_say($say_hello);
    $l($name);
};

$logging_hello("John");