DelegateClass:delegatedMethod is called.
As can be seen from the above example, TestClass does not declare the printMessage member method, but it is passed directly to the delegate object through the clever bridging of the __call() method. I personally think this technique is a double-edged sword and should not be overused.
5. Callback function:
There is no need to describe the application scenarios of callback functions. There are countless typical use cases of callback functions in C/C++. Here we simply give the rules for using callback functions in PHP. See the following sample code and key comments:
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}
class ProcessSale {
private $callbacks;
function registerCallback($cb) {
if (!is_callable($cb)) {
throw new Exception("callback not callable.");
}
$this->callbacks[] = $cb;
}
function sale($product) {
print "{$product->name}: processing n";
foreach ($this->callbacks as $cb) {
//The following two calling methods are available.
call_user_func($cb, $product);
$cb($product);
}
}
}
$logger = function($product) {
print " logging ({$product->name})n";
};
$processor = new ProcessSale();
$processor->registerCallback($logger);
$processor->sale(new Product("shoes",6));
print "n";
$processor->sale(new Product("coffee",6));
Copy code
The running results are as follows:
Copy code
Stephens-Air:Desktop$ php Test.php
shoes: processing
logging (shoes)
logging (shoes)
coffee: processing
logging (coffee)
logging (coffee)
Copy code
6. use(closure):
There are a large number of closure applications in Javascript, and closures in PHP are implemented through the use keyword. As for the concept of closure itself, simply speaking, code within a function can access variables in its parent scope. See the following sample code and key comments:
Copy code
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}
class ProcessSale {
private $callbacks;
function registerCallback($cb) {
if (!is_callable($cb)) {
throw new Exception("callback not callable.");
}
$this->callbacks[] = $cb;
}
function sale($product) {
print "{$product->name}: processing n";
foreach ($this->callbacks as $cb) {
$cb($product);
}
}
}
class Totalizer {
static function warnAmount($amt) {
$count = 0;
//注意这里的$amt和$count均为闭包变量,其中&$count是以引用的形式传递的,即一旦函数内部修改了该变量的值,
//那么下次再访问该闭包变量时,$count将为之前调用中修改后的值。
return function($product) use($amt, &$count) {
$count += $product->price;
print " count: $countn";
if ($count > $amt) {
print " high price reached: {$count}n";
}
};
}
}
$processor = new ProcessSale();
$processor->registerCallback(Totalizer::warnAmount(8));
$processor->sale(new Product("shoes",6));
$processor->sale(new Product("coffee",6));
复制代码
运行结果如下:
shoes: processing
count: 6
coffee: processing
count: 12
high price reached: 12
注:该Blog中记录的知识点,是在我学习PHP的过程中,遇到的一些PHP和其他面向对象语言相比比较独特的地方,或者是对我本人而言确实需要簿记下来以备后查的知识点。虽然谈不上什么深度,但是还是希望能与大家分享。
http://www.bkjia.com/PHPjc/635016.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/635016.htmlTechArticle1. __toString: 当对象被打印时,如果该类定义了该方法,则打印该方法的返回值,否则将按照PHP的缺省行为输出打印结果。该方法类似于Java中...