这里的构造函数居然这样写。
<meta charset="utf-8">
<?php
function Person($data = array())
{
{/* __construct */
static $i = 0;
++$i;
extract(array_merge(array('name' => '未命名') /* 默认值 */, $data));
}
return array(
'Show' => function() use($name, $i)
{
echo "装扮的{$name}, 第{$i}个Person对象";
}
);
}
function Finery($component)
{
return array(
'Show' => function() use($component)
{
$component['Show']();
}
);
}
function TShirts($component)
{
$f_parent = Finery($component);
return array(
'Show' => function() use($f_parent)
{
echo '大T恤 ';
$f_parent['Show']();
}
);
}
function BigTrouser($component)
{
$f_parent = Finery($component);
return array(
'Show' => function() use($f_parent)
{
echo '垮裤 ';
$f_parent['Show']();
}
);
}
$p = Person(array('name' => '额'));
$p = Person();
$bt = BigTrouser($p);
$ts = TShirts($bt);
$ts['Show']();
/* f_finery.php */
面向对象
<meta charset="utf-8">
<?php
class Person
{
private $name;
function __construct($name = '')
{
if($name !== '') $this->name = $name;
}
public function Show()
{
echo '装扮的' . $this->name;
}
}
class Finery extends Person
{
protected $component;
//打扮
public function Decorate(Person $component_)
{
$this->component = $component_;
}
public function Show()
{
if ($this->component != null)
{
$this->component->Show();
}
}
}
class TShirts extends Finery
{
public function Show()
{
echo '大T恤 ';
parent::Show();
}
}
class BigTrouser extends Finery
{
public function Show()
{
echo '垮裤 ';
parent::Show();
}
}
$p = new Person('额');
$bt = new BigTrouser();
$ts = new TShirts();
$bt->Decorate($p);
$ts->Decorate($bt);
$ts->Show();
/* c_finery.php */
实际上,用oo或者仿oo,还是复杂了些,最简单的说明就是。
$fn = function() {
echo ' body ';
};
$fn = function() use($fn) {
echo 'head ';
$fn();
echo ' footer';
};
$fn();