匿名函数-

<?php

class Product {
    public $name;
    public $price;

    function __construct($name, $price){
        $this->name  = $name;
        $this->price = $price;
    }
}

class ProcessSale {
    private $callbacks;

    function registerCallback( $callback){
        if(! is_callable( $callback)){
            throw new Exception("callback not callable");
        }
        $this->callbacks[] = $callback;
    }

    function  sale($product){
        print "{$product->name} : processing \n";
        foreach ($this->callbacks as $callback){
            call_user_func($callback,$product);
        }
    }
}
//?? ?????????????????????????????????
//     ?????????????????????????????

$logger = create_function('$product', 'print " logging({$product->name})\n";');
//create_function ??????
$processor =new ProcessSale();
$processor->registerCallback($logger);
$processor->sale(new Product("shoes", 6));
print "\n";
$processor->sale(new Product("coffee", 6));

//??? php5.3+
print "-------------------------\n";
$logger2 = function($product){
  print "loggin({$product->name}) \n";
};
$processor =new ProcessSale();
$processor->registerCallback($logger2);
$processor->sale(new Product("shoes2", 3));
print "\n";
$processor->sale(new Product("coffee2", 3));

print "-------------------------\n";

猜你喜欢

转载自blog.csdn.net/Linux_Daemon/article/details/84135000