# 组合模式
组合(Composite)模式:有时又叫作部分-整体模式将。将对象组合成树状层次结构,使用户对单个对象和组合对象具有一致的访问性。
**主要优点有:**
1. 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;
2. 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;
**主要缺点是:**
1. 设计较复杂,客户端需要花更多时间理清类之间的层次关系;
2. 不容易限制容器中的构件;
3. 不容易用继承的方法来增加构件的新功能;
常见使用场景:如树形菜单、文件夹菜单、部门组织架构图、窗体程序中的简单控件与容器控件等
#### **模式的结构**
组合模式包含以下主要角色。
1. \*\* 抽象构件(Component)角色\*\*:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。
2. **树叶构件(Leaf)角色**:是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中 声明的公共接口。
3. **树枝构件(Composite)角色**:是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。
组合模式分为透明式的组合模式和安全式的组合模式。
**透明式的组合模式**
```
<pre class="calibre10">```
<span class="token">/**
*透明式合成模式
*抽象构件
*/</span>
interface <span class="token4">Component</span> <span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">/**
* 树枝构件
*/</span>
class <span class="token4">Composite</span> implements <span class="token4">Component</span> <span class="token3">{</span>
private $_children_composites<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//返回自己的实例</span>
public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
<span class="token5">return</span> $this<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">// 示例方法,调用各个子对象的operation方法</span>
public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_children_composites as $composite<span class="token3">)</span> <span class="token3">{</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//聚集管理方法 添加一个子对象</span>
public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>_children_composites<span class="token3">[</span><span class="token3">]</span> <span class="token1">=</span> $component<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//聚集管理方法 删除一个子对象</span>
public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_children_composites as $key <span class="token1">=</span><span class="token1">></span> $row<span class="token3">)</span> <span class="token3">{</span>
<span class="token5">if</span> <span class="token3">(</span>$component <span class="token1">==</span> $row<span class="token3">)</span> <span class="token3">{</span>
<span class="token4">unset</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_children_composites<span class="token3">[</span>$key<span class="token3">]</span><span class="token3">)</span><span class="token3">;</span>
<span class="token5">return</span> TRUE<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token5">return</span> FALSE<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">// 聚集管理方法 返回所有的子对象</span>
public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
<span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>_children_composites<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">/**
* 树叶构件
*/</span>
class <span class="token4">Leaf</span> implements <span class="token4">Component</span> <span class="token3">{</span>
private $_name<span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span> <span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>_name <span class="token1">=</span> $name<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
echo $this<span class="token1">-</span><span class="token1">></span>_name<span class="token3">.</span><span class="token2">"<br>"</span><span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
<span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span>
<span class="token5">return</span> FALSE<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span>
<span class="token5">return</span> FALSE<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
<span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span><span class="token">//树叶构件无子节点</span>
<span class="token3">}</span>
<span class="token3">}</span>
class <span class="token4">CompositePattern</span><span class="token3">{</span>
public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token">// client </span>
$leaf1 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'first'</span><span class="token3">)</span><span class="token3">;</span>
$leaf2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'second'</span><span class="token3">)</span><span class="token3">;</span>
$composite <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf1<span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">remove</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
CompositePattern<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
```
```
**安全式的组合模式**
```
<pre class="calibre10">```
<span class="token">/**
*
*安全式合成模式
*/</span>
interface <span class="token4">Component</span> <span class="token3">{</span>
public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//返回自己的实例</span>
public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
class <span class="token4">Composite</span> implements <span class="token4">Component</span> <span class="token3">{</span> <span class="token">// 树枝组件角色</span>
private $_composites<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $this<span class="token3">;</span> <span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_composites as $composite<span class="token3">)</span> <span class="token3">{</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> <span class="token">//聚集管理方法 添加一个子对象</span>
$this<span class="token1">-</span><span class="token1">></span>_composites<span class="token3">[</span><span class="token3">]</span> <span class="token1">=</span> $component<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">remove</span><span class="token3">(</span>Component $component<span class="token3">)</span> <span class="token3">{</span> <span class="token">// 聚集管理方法 删除一个子对象</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_composites as $key <span class="token1">=</span><span class="token1">></span> $row<span class="token3">)</span> <span class="token3">{</span>
<span class="token5">if</span> <span class="token3">(</span>$component <span class="token1">==</span> $row<span class="token3">)</span> <span class="token3">{</span> <span class="token4">unset</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>_composites<span class="token3">[</span>$key<span class="token3">]</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">return</span> TRUE<span class="token3">;</span> <span class="token3">}</span>
<span class="token3">}</span>
<span class="token5">return</span> FALSE<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">getChild</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 聚集管理方法 返回所有的子对象</span>
<span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>_composites<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
class <span class="token4">Leaf</span> implements <span class="token4">Component</span> <span class="token3">{</span>
private $_name<span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span> <span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>_name <span class="token1">=</span> $name<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
echo $this<span class="token1">-</span><span class="token1">></span>_name<span class="token3">.</span><span class="token2">"被访问了<br>"</span><span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span>
<span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
class <span class="token4">CompositePattern</span><span class="token3">{</span>
public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token">// client</span>
$leaf1 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'first'</span><span class="token3">)</span><span class="token3">;</span>
$leaf2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">'second'</span><span class="token3">)</span><span class="token3">;</span>
$composite <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf1<span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
echo <span class="token2">"-----------<br />"</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">remove</span><span class="token3">(</span>$leaf2<span class="token3">)</span><span class="token3">;</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">operation</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
CompositePattern<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
结果:
first被访问了
second被访问了
<span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">-</span>
first被访问了
```
```
## **组合模式的应用场景**
1. 在需要表示一个对象整体与部分的层次结构的场合。
2. 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。
```
<pre class="calibre10">```
<span class="token">//抽象构件</span>
Abstract class <span class="token4">Component</span><span class="token3">{</span>
public $name<span class="token3">;</span>
abstract <span class="token5">function</span> <span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>$name<span class="token3">)</span><span class="token3">{</span>
$this<span class="token1">-</span><span class="token1">></span>name <span class="token1">=</span> $name<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//总经理 部门经理 主管等 树枝构件</span>
class <span class="token4">Composite</span> extends <span class="token4">Component</span><span class="token3">{</span>
public $lever <span class="token1">=</span> <span class="token6">1</span><span class="token3">;</span>
public $c_nodes <span class="token1">=</span> <span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
echo <span class="token2">""</span><span class="token3">;</span>
<span class="token4">print_r</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>c_nodes<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//添加子节点</span>
public <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>Component $component<span class="token3">)</span><span class="token3">{</span>
$component<span class="token1">-</span><span class="token1">></span>lever <span class="token1">=</span> $this<span class="token1">-</span><span class="token1">></span>lever <span class="token1">+</span> <span class="token6">1</span><span class="token3">;</span>
$this<span class="token1">-</span><span class="token1">></span>c_nodes<span class="token3">[</span><span class="token3">]</span> <span class="token1">=</span> $component<span class="token3">;</span>
<span class="token3">}</span>
public <span class="token5">function</span> <span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
$nbsp<span class="token1">=</span><span class="token4">str_repeat</span><span class="token3">(</span><span class="token2">" "</span><span class="token3">,</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>lever<span class="token1">-</span><span class="token6">1</span><span class="token3">)</span><span class="token1">*</span><span class="token6">4</span><span class="token3">)</span><span class="token3">;</span>
echo <span class="token2">"{$nbsp}我是层级{$this->lever}:{$this->name}<br>"</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//普通员工 树叶构件 不能添加子节点</span>
class <span class="token4">Leaf</span> extends <span class="token4">Component</span><span class="token3">{</span>
public $lever<span class="token3">;</span>
public <span class="token5">function</span> <span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
$nbsp<span class="token1">=</span><span class="token4">str_repeat</span><span class="token3">(</span><span class="token2">" "</span><span class="token3">,</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>lever<span class="token1">-</span><span class="token6">1</span><span class="token3">)</span><span class="token1">*</span><span class="token6">4</span><span class="token3">)</span><span class="token3">;</span>
echo <span class="token2">"{$nbsp}层级{$this->lever}:{$this->name}(work)<br>"</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
$manager <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token2">"总经理"</span><span class="token3">)</span><span class="token3">;</span>
$xgm <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token2">"销售经理"</span><span class="token3">)</span><span class="token3">;</span>
$sgm <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Composite</span><span class="token3">(</span><span class="token2">"行政经理"</span><span class="token3">)</span><span class="token3">;</span>
$staff <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">"张三"</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//组装成树</span>
$manager<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$xgm<span class="token3">)</span><span class="token3">;</span>
$manager<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$sgm<span class="token3">)</span><span class="token3">;</span>
$sgm<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$staff<span class="token3">)</span><span class="token3">;</span>
$staff2 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">"李四"</span><span class="token3">)</span><span class="token3">;</span>
$sgm<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$staff2<span class="token3">)</span><span class="token3">;</span>
$staff3 <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Leaf</span><span class="token3">(</span><span class="token2">"王五"</span><span class="token3">)</span><span class="token3">;</span>
$xgm<span class="token1">-</span><span class="token1">></span><span class="token4">add</span><span class="token3">(</span>$staff3<span class="token3">)</span><span class="token3">;</span>
$manager<span class="token1">-</span><span class="token1">></span><span class="token4">getChildComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//遍历树 - 函数</span>
<span class="token5">function</span> <span class="token4">display</span><span class="token3">(</span>Composite $composite<span class="token3">)</span><span class="token3">{</span>
$composite<span class="token1">-</span><span class="token1">></span><span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token4">foreach</span><span class="token3">(</span>$composite<span class="token1">-</span><span class="token1">></span>c_nodes as $c_node<span class="token3">)</span><span class="token3">{</span>
<span class="token">//属于枝叶执行doSomething 否则继续回调displa函数</span>
$c_node <span class="token5">instanceof</span> <span class="token4">Leaf</span> <span class="token1">?</span> $c_node<span class="token1">-</span><span class="token1">></span><span class="token4">doSomething</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">:</span> <span class="token4">display</span><span class="token3">(</span>$c_node<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token4">display</span><span class="token3">(</span>$manager<span class="token3">)</span><span class="token3">;</span>
```
```
结果:
```
<pre class="calibre10">```
Array
<span class="token3">(</span>
<span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Composite Object
<span class="token3">(</span>
<span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">2</span>
<span class="token3">[</span>c_nodes<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Array
<span class="token3">(</span>
<span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Leaf Object
<span class="token3">(</span>
<span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">3</span>
<span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 王五
<span class="token3">)</span>
<span class="token3">)</span>
<span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 销售经理
<span class="token3">)</span>
<span class="token3">[</span><span class="token6">1</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Composite Object
<span class="token3">(</span>
<span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">2</span>
<span class="token3">[</span>c_nodes<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Array
<span class="token3">(</span>
<span class="token3">[</span><span class="token6">0</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Leaf Object
<span class="token3">(</span>
<span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">3</span>
<span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 张三
<span class="token3">)</span>
<span class="token3">[</span><span class="token6">1</span><span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> Leaf Object
<span class="token3">(</span>
<span class="token3">[</span>lever<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> <span class="token6">3</span>
<span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 李四
<span class="token3">)</span>
<span class="token3">)</span>
<span class="token3">[</span>name<span class="token3">]</span> <span class="token1">=</span><span class="token1">></span> 行政经理
<span class="token3">)</span>
<span class="token3">)</span>
我是层级<span class="token6">1</span><span class="token3">:</span>总经理
我是层级<span class="token6">2</span><span class="token3">:</span>销售经理
层级<span class="token6">3</span><span class="token3">:</span>王五(work)
我是层级<span class="token6">2</span><span class="token3">:</span>行政经理
层级<span class="token6">3</span><span class="token3">:</span>张三(work)
层级<span class="token6">3</span><span class="token3">:</span>李四(work)
```
```
实例2:

```
<pre class="calibre10">```
<span class="token1"><</span><span class="token1">?</span>php
<span class="token">//模型中所有的类都扩展自 Unit 类,同时,Army 和 TroopCarrier 类被设计成了组合对象,用于包含 Unit 对象。</span>
<span class="token">//但是,我们也发现 ArcherUnit 和 LaserCannonUnit 类(称为局部对象,也称为树叶对象,因为组合模式为树形结构,组合对象为树干,单独存在的对象为树叶,树叶对象应为最小单元,其中不能包含其他对象。)不能包含其他 Unit 对象,但也实现了addUnit() 方法,这个问题我们后面讨论。</span>
<span class="token">/* 战斗单元的抽象类
有一个抽象方法bombardStrength()方法用于设置战斗单元的攻击强度
*/</span>
abstract class <span class="token4">Unit</span><span class="token3">{</span>
<span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> <span class="token5">null</span><span class="token3">;</span>
<span class="token3">}</span>
abstract <span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//弓箭手</span>
class <span class="token4">Archer</span> extends <span class="token4">Unit</span><span class="token3">{</span>
<span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> <span class="token6">4</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//激光炮单位</span>
class <span class="token4">LaserCanonUnit</span> extends <span class="token4">Unit</span><span class="token3">{</span>
<span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> <span class="token6">44</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">/* CompositeUnit 混合作战单元类
因为CompositeUnit没有实现bombardStrength()方法,
所以CompositeUnit声明为抽象类abstract
战斗单元为树叶类时为了避免使用addUnit与removeUnit则通过下面抽象出的CompositeUnit类的getComposite
*/</span>
abstract class <span class="token4">CompositeUnit</span> extends <span class="token4">Unit</span><span class="token3">{</span>
private $units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token5">function</span> <span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> $this<span class="token3">;</span>
<span class="token3">}</span>
protected <span class="token5">function</span> <span class="token4">units</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">;</span>
<span class="token3">}</span>
<span class="token5">function</span> <span class="token4">removeUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span>
$units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>units as $thisunit<span class="token3">)</span><span class="token3">{</span>
<span class="token5">if</span><span class="token3">(</span><span class="token1">!</span>unit<span class="token1">!==</span>$thisunit<span class="token3">)</span><span class="token3">{</span>
$units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$thisunit<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
$this<span class="token1">-</span><span class="token1">></span>units<span class="token1">=</span>$units<span class="token3">;</span>
<span class="token3">}</span>
<span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span>
<span class="token5">if</span><span class="token3">(</span><span class="token4">in_array</span><span class="token3">(</span>$unit<span class="token3">,</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">,</span><span class="token6">true</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span><span class="token3">;</span>
<span class="token3">}</span>
$this<span class="token1">-</span><span class="token1">></span>units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$unit<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">/**
* 定义一个独立的类(Army类)来组合战斗单元。
* 战斗集合的类 树枝类
* 如果 Army(军队)类需要和其他Army类进行合并,同时,每个 Army 都有自己的 ID,这样 Army 在以后还可以从整编中解散出来。
* 我们修改原来的 Army(军队)类,使其同时支持 Unit(单元)和 Army(军队)。
*/</span>
class <span class="token4">Army</span> extends <span class="token4">CompositeUnit</span><span class="token3">{</span>
public $units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//添加单位</span>
<span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span>
<span class="token5">if</span><span class="token3">(</span><span class="token4">in_array</span><span class="token3">(</span>$unit<span class="token3">,</span> $this<span class="token1">-</span><span class="token1">></span>units<span class="token3">,</span><span class="token6">true</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span><span class="token3">;</span>
<span class="token3">}</span>
$this<span class="token1">-</span><span class="token1">></span>units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$unit<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//移除单位</span>
<span class="token5">function</span> <span class="token4">removeUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span>
$units<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>units as $thisunit<span class="token3">)</span><span class="token3">{</span>
<span class="token5">if</span><span class="token3">(</span>$unit<span class="token1">!==</span>$thisunit<span class="token3">)</span><span class="token3">{</span>
$units<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$thisunit<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
$this<span class="token1">-</span><span class="token1">></span>units<span class="token1">=</span>$thisunit<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//总攻击强度</span>
<span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
$ret<span class="token1">=</span><span class="token6">0</span><span class="token3">;</span>
foreach <span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>units as $unit<span class="token3">)</span><span class="token3">{</span>
$ret<span class="token1">+</span><span class="token1">=</span>$unit<span class="token1">-</span><span class="token1">></span><span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token5">return</span> $ret<span class="token3">;</span>
<span class="token3">}</span>
<span class="token">//添加陆军</span>
<span class="token5">function</span> <span class="token4">addArmy</span><span class="token3">(</span>Army $army<span class="token3">)</span><span class="token3">{</span>
<span class="token4">array_push</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>armies<span class="token3">,</span> $army<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//运兵船</span>
class <span class="token4">TroopCarrier</span> extends <span class="token4">CompositeUnit</span><span class="token3">{</span>
<span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span>
<span class="token5">if</span><span class="token3">(</span>$unit <span class="token5">instanceof</span> <span class="token4">Cavalry</span><span class="token3">)</span><span class="token3">{</span><span class="token">//骑兵对象</span>
<span class="token5">throw</span> <span class="token5">new</span> <span class="token4">UnitException</span><span class="token3">(</span><span class="token2">"不能将骑兵带上船"</span><span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
Parent<span class="token3">:</span><span class="token3">:</span><span class="token4">addUnit</span><span class="token3">(</span>$unit<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token5">return</span> <span class="token6">0</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
<span class="token">//骑兵对象</span>
class <span class="token4">Cavalry</span> extends <span class="token4">CompositeUnit</span><span class="token3">{</span>
<span class="token5">function</span> <span class="token4">addUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span>
<span class="token3">}</span>
<span class="token5">function</span> <span class="token4">removeUnit</span><span class="token3">(</span>Unit $unit<span class="token3">)</span><span class="token3">{</span>
<span class="token3">}</span>
<span class="token5">function</span> <span class="token4">bombardStrength</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span>
<span class="token3">}</span>
<span class="token3">}</span>
class <span class="token4">UnitException</span> extends <span class="token4">Exception</span><span class="token3">{</span>
<span class="token3">}</span>
class <span class="token4">UnitScript</span><span class="token3">{</span>
<span class="token">//有两个Unit类型的参数,第一个是新的Unit对象,第二个是之前的Unit对象</span>
static <span class="token5">function</span> <span class="token4">joinExisting</span><span class="token3">(</span>Unit $newUnit<span class="token3">,</span>Unit $oldUnit<span class="token3">)</span><span class="token3">{</span>
$comp<span class="token3">;</span>
<span class="token5">if</span><span class="token3">(</span><span class="token1">!</span><span class="token4">is_null</span><span class="token3">(</span>$comp<span class="token1">=</span>$oldUnit<span class="token1">-</span><span class="token1">></span><span class="token4">getComposite</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">)</span><span class="token3">{</span>
<span class="token">//如果第二个Unit对象是一个CompositeUnit对象,那么直接add</span>
$comp<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$newUnit<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span><span class="token5">else</span> <span class="token3">{</span>
<span class="token">//如果第二个Unit对象不是一个CompositeUnit对象,那么创建一个Army对象,将两个Unit存入这个Army对象 </span>
$comp<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$comp<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$oldUnit<span class="token3">)</span><span class="token3">;</span>
$comp<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$newUnit<span class="token3">)</span><span class="token3">;</span>
<span class="token3">}</span>
<span class="token5">return</span> $comp<span class="token3">;</span>
<span class="token3">}</span>
<span class="token3">}</span>
$main_army<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$main_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$main_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">LaserCanonUnit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$sub_army<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$sub_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$sub_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$sub_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$main_army<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span>$sub_army<span class="token3">)</span><span class="token3">;</span>
$a<span class="token1">=</span><span class="token5">new</span> <span class="token4">TroopCarrier</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$b<span class="token1">=</span>$a<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
<span class="token">//$b=$a->addUnit(new Cavalry());</span>
<span class="token">//print_r($main_army->units);</span>
print <span class="token2">"攻击强度:{$main_army->bombardStrength()}<br>"</span><span class="token3">;</span>
<span class="token">//也可以这样 </span>
UnitScript<span class="token3">:</span><span class="token3">:</span><span class="token4">joinExisting</span><span class="token3">(</span>$main_army<span class="token3">,</span> $sub_army<span class="token3">)</span><span class="token3">;</span>
$main<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$main<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">Archer</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$sub<span class="token1">=</span><span class="token5">new</span> <span class="token4">Army</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span>
$sub<span class="token1">-</span><span class="token1">></span><span class="token4">addUnit</span><span class="token3">(</span><span class="token5">new</span> <span class="token4">LaserCanonUnit</span><span class="token3">(</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span>
$all<span class="token1">=</span>UnitScript<span class="token3">:</span><span class="token3">:</span><span class="token4">joinExisting</span><span class="token3">(</span>$main<span class="token3">,</span> $sub<span class="token3">)</span><span class="token3">;</span>
print <span class="token2">"攻击强度:{$all->bombardStrength()}<br>"</span><span class="token3">;</span>
```
```
结果:
```
<pre class="calibre17">```
攻击强度:<span class="token6">60</span>
攻击强度:<span class="token6">48</span>
```
```
- php更新内容
- 其他
- empty、isset、is_null
- echo 输出bool值
- if真假情况
- 常量
- define与const(php5.3) 类常量
- 递归
- 单元测试
- 面向对象
- 全局变量域超全局变量
- php网络相关
- 支持的协议和封装协议(如http,php://input)
- 上下文(Context)选项和参数
- 过滤器
- http请求及模拟登录
- socket
- streams
- swoole
- 超全局变量
- $_ENV :存储了一些系统的环境变量
- $_COOKIE
- $_SESSION
- $_FILES
- $_SERVER
- 正则
- php正则函数
- 去除文本中的html、xml的标签
- 特殊符号
- \r\n
- 模式修正符
- 分组
- 断言
- 条件表达式
- 递归表达式 (?R)
- 固化分组
- 正则例子
- 框架
- 自动加载spl_autoload_register
- 时间函数
- 文件操作
- 文件的上传下载
- 常见的mimi类型
- 文件断点续传
- 下载文件防盗链
- 破解防盗链
- 无限分类
- 短信验证码
- 短信宝
- 视频分段加载
- phpDoc注释
- 流程控制代替语法
- 三元运算
- @错误抑制符
- 字符编码
- PHP CLI模式开发
- 配置可修改范围
- CGI、FastCGI和PHP-FPM关系图解
- No input file specified的解决方法
- SAPI(PHP常见的四种运行模式)
- assert断言
- 类基础
- 类的三大特性:封装,继承,多态
- 魔术方法
- 辅助查询(*)
- extends继承
- abstract 抽象类
- interface 接口(需要implements实现)
- 抽象类和接口的区别
- 多态
- static
- final
- serialize与unserialize
- instanceof 判断后代子类
- 类型约束
- clone克隆
- ::的用法
- new self()与new static()
- this、self、static、parent、super
- self、static、parent:后期静态绑定
- PHP的静态变量
- php导入
- trait
- 动态调用类方法
- 参数及类型申明
- 方法的重载覆盖
- return $a && $b
- 设计思想
- 依赖注入与依赖倒置
- 创建型模式(创建类对象)
- (*)单例模式
- (*)工厂模式
- 原型模式(在方法里克隆this)
- 创建者模式
- 结构型模式
- 适配器模式(Adapter)
- 桥接模式
- 装饰模式
- 组合模式
- 外观模式(门面(Facade)模式)
- 享元模式
- 代理模式
- 行为型模式
- (*)观察者模式
- (*)迭代器模式(Iterator)
- 模板方法模式 Template
- 命令模式(Command)
- 中介者模式(Mediator)
- 状态模式(State)
- 职责链模式 (Chainof Responsibility)
- 策略模式(Strategy)
- 已知模式-备忘录模式(Memento)
- 深度模式-解释器模式(Interpreter)
- 深度模式-访问者模式(Visitor)
- (*)注册树(注射器、注册表)模式
- 函数参考
- 影响 PHP 行为的扩展
- APC扩展(过时)
- APCu扩展
- APD扩展(过时)
- bcompiler扩展(过时)
- BLENC扩展 (代码加密 实验型)
- Componere扩展(7.1+)
- 错误处理扩展(PHP 核心)
- FFI扩展
- htscanner扩展
- inclued扩展
- Memtrack扩展
- OPcache扩展(5.5.0内部集成)
- Output Control扩展(核心)
- PHP Options/Info扩展(核心)
- phpdbg扩展(5.6+内部集成)
- runkit扩展
- runkit7扩展
- scream扩展
- uopz扩展
- Weakref扩展
- WinCache扩展
- Xhprof扩展
- 音频格式操作
- ID3
- KTaglib
- oggvorbis
- OpenAL
- 身份认证服务
- KADM5
- Radius
- 针对命令行的扩展
- Ncurses(暂无人维护)
- Newt(暂无人维护)
- Readline
- 压缩与归档扩展
- Bzip2
- LZF
- Phar
- Rar
- Zip
- Zlib
- 信用卡处理
- 加密扩展
- Crack(停止维护)
- CSPRNG(核心)
- Hash扩展(4.2内置默认开启、7.4核心)
- Mcrypt(7.2移除)
- Mhash(过时)
- OpenSSL(*)
- 密码散列算法(核心)
- Sodium(+)
- 数据库扩展
- 数据库抽象层
- 针对各数据库系统对应的扩展
- 日期与时间相关扩展
- Calendar
- 日期/时间(核心)
- HRTime(*)
- 文件系统相关扩展
- Direct IO
- 目录(核心)
- Fileinfo(内置)
- 文件系统(核心)
- Inotify
- Mimetype(过时)
- Phdfs
- Proctitle
- xattr
- xdiff
- 国际化与字符编码支持
- Enchant
- FriBiDi
- Gender
- Gettext
- iconv(内置默认开启)
- intl
- 多字节字符串(mbstring)
- Pspell
- Recode(将要过时)
- 图像生成和处理
- Cairo
- Exif
- GD(内置)
- Gmagick
- ImageMagick
- 邮件相关扩展
- Cyrus
- IMAP
- Mail(核心)
- Mailparse
- vpopmail(实验性 )
- 数学扩展
- BC Math
- GMP
- Lapack
- Math(核心)
- Statistics
- Trader
- 非文本内容的 MIME 输出
- FDF
- GnuPG
- haru(实验性)
- Ming(实验性)
- wkhtmltox(*)
- PS
- RPM Reader(停止维护)
- RpmInfo
- XLSWriter Excel操作(*)
- 进程控制扩展
- Eio
- Ev
- Expect
- Libevent
- PCNTL
- POSIX
- 程序执行扩展(核心)
- parallel
- pthreads(*)
- pht
- Semaphore
- Shared Memory
- Sync
- 其它基本扩展
- FANN
- GeoIP(*)
- JSON(内置)
- Judy
- Lua
- LuaSandbox
- Misc(核心)
- Parsekit
- SeasLog(-)
- SPL(核心)
- SPL Types(实验性)
- Streams(核心)
- Swoole(*)
- Tidy扩展
- Tokenizer
- URLs(核心)
- V8js(*)
- Yaml
- Yaf
- Yaconf(核心)
- Taint(检测xss字符串等)
- Data Structures
- 其它服务
- 网络(核心)
- cURL(*)
- Event(*)
- chdb
- FAM
- FTP
- Gearman
- Gopher
- Gupnp
- Hyperwave API(过时)
- LDAP(+)
- Memcache
- Memcached(+)
- mqseries
- RRD
- SAM
- ScoutAPM
- SNMP
- Sockets
- SSH2
- Stomp
- SVM
- SVN(试验性的)
- TCP扩展
- Varnish
- YAZ
- YP/NIS
- 0MQ(ZeroMQ、ZMQ)消息系统
- ZooKeeper
- 搜索引擎扩展
- mnoGoSearch
- Solr
- Sphinx
- Swish(实验性)
- 针对服务器的扩展
- Apache
- FastCGI 进程管理器
- IIS
- NSAPI
- Session 扩展
- Msession
- Sessions
- Session PgSQL
- 文本处理
- BBCode
- CommonMark(markdown解析)
- Parle
- PCRE( 核心)
- POSIX Regex
- ssdeep
- 字符串(核心)
- 变量与类型相关扩展
- 数组(核心)
- 类/对象(核心)
- Classkit(未维护)
- Ctype
- Filter扩展
- 函数处理(核心)
- quickhash扩展
- 反射扩展(核心)
- Variable handling(核心)
- Web 服务
- OAuth
- SCA(实验性)
- SOAP
- Yar
- XML-RPC(实验性)
- Windows 专用扩展
- COM
- win32ps
- win32service
- XML 操作
- DOM(内置,默认开启)
- libxml(内置 默认开启)
- SDO(停止维护)
- SDO-DAS-Relational(试验性的)
- SDO DAS XML
- SimpleXML(内置,5.12+默认开启)
- WDDX
- XMLDiff
- XML 解析器(Expat 解析器 默认开启)
- XMLReader(5.1+内置默认开启)
- XMLWriter(5.1+内置默认开启)
- XSL(内置)
- 图形用户界面(GUI) 扩展
- UI
- 预定义类
- PHP SPL(PHP 标准库)
- 数据结构
- SplDoublyLinkedList(双向链表)
- SplStack(栈 先进后出)
- SplQueue(队列)
- SplHeap(堆)
- SplMaxHeap(最大堆)
- SplMinHeap(最小堆)
- SplPriorityQueue(堆之优先队列)
- SplFixedArray(阵列【数组】)
- SplObjectStorage(映射【对象存储】)
- 迭代器
- DirectoryIterator类
- 文件处理
- SplFileInfo
- SplFileObject
- SplTempFileObject
- 接口 interface
- Countable
- OuterIterator
- RecursiveIterator
- SeekableIterator
- 异常
- 各种类及接口
- SplSubject
- SplObserver
- ArrayObject(将数组作为对象操作)
- SPL 函数
- 预定义接口
- Traversable(遍历)接口
- Iterator(迭代器)接口
- IteratorAggregate(聚合式迭代器)接口
- ArrayAccess(数组式访问)接口
- Serializable 序列化接口
- JsonSerializable
- Closure 匿名函数(闭包)类
- Generator生成器类
- 生成器(php5.5+)
- 反射
- 一、反射(reflection)类
- 二、Reflector 接口
- ReflectionClass 类报告了一个类的有关信息。
- ReflectionFunctionAbstract
- ReflectionParameter 获取函数或方法参数的相关信息
- ReflectionProperty 类报告了类的属性的相关信息。
- ReflectionClassConstant类报告有关类常量的信息。
- ReflectionZendExtension 类返回Zend扩展相关信息
- ReflectionExtension 报告了一个扩展(extension)的有关信息。
- 三、ReflectionGenerator类用于获取生成器的信息
- 四、ReflectionType 类用于获取函数、类方法的参数或者返回值的类型。
- 五、反射的应用场景
- git
- Git代码同时上传到GitHub和Gitee(码云)
- Git - 多人协同开发利器,团队协作流程规范与注意事项
- 删除远程仓库的文件
- 创建composer项目
- composer安装及设置
- composer自动加载讲解
- phpsdudy的composer操作
- swoole笔记
- 安装及常用Cli操作
- TCP
- 4种回调函数的写法
- phpRedis
- API
- API详细
- redis DB 概念:
- 通用命令:rawCommand
- Connection
- Server
- List
- Set
- Zset
- Hash
- string
- Keys
- 事物
- 发布订阅
- 流streams
- Geocoding 地理位置
- lua脚本
- Introspection 自我检测
- biMap
- 原生
- php-redis 操作类 封装
- redis 队列解决秒杀解决超卖:
- Linux
- Centos8(Liunx) 中安装PHP7.4 的三种方法和删除它的三种方法
- 权限设计
- ACL
- RBAC
- RBAC0
- RBAC1
- RBAC2
- RBAC3
- 例子
- ABAC 基于属性的访问控制
- 总结:SAAS后台权限设计案例分析
- casbin-权限管理框架
- 开始使用
- casbinAPI
- Think-Casbin
- 单点登录(SSO)
- OAuth授权
- OAuth 2.0 的四种方式
- 更新令牌
- 例子:第三方登录
- 微服务架构下的统一身份认证和授权
- 杂项
- SSL证书
- sublime Emmet的快捷语法
- 免费翻译接口
- 免费空间
- xss过滤
- HTML Purifier文档
- xss例子
- 实用小函数
- PHP操作Excel
- 架构师必须知道的26项PHP安全实践
- 模版布局
- smarty模版
- blade
- twig
- 大佬博客
- 优化
- 缓存
- opcache
- memcache
- 数据库
- 主从分布
- 数据库设计
- 笔记
- 配置
- nginx 主从配置
- nginx 负载均衡的配置
- 手动搭建Redis集群和MySQL主从同步(非Docker)
- Redis Cluster集群
- mysql主从同步
- 用安卓手机搭建 web 服务器
- 软件选择
- 扩展库列表
- 代码审计
- 漏洞挖掘的思路
- 命令注入
- 代码注入
- XSS 反射型漏洞
- XSS 存储型漏洞
- 本地包含与远程包含
- sql注入
- 函数
- 注释
- 步骤
- information_schema
- sql注入的分类
- 实战
- 防御
- CSRF 跨站请求伪造
- 计动态函数执行与匿名函数执行
- unserialize反序列化漏洞
- 覆盖变量漏洞
- 文件管理漏洞
- 文件上传漏洞
- 跳过登录
- URL编码对照表
- 浏览器插件开发
- 插件推荐
- 扩展文件manifest.json
- 不可视的background(常驻)页面
- 可视页面browser actions与page actions及八种展示方式
- 使用chrome.xxx API
- Google Chrome扩展与Web页面/服务器之间的交互
- Google Chrome扩展中的页面之间的数据通信
- inject-script
- chromeAPI
- pageAction
- alarms
- chrome.tabs
- chrome.runtime
- chrome.webRequest
- chrome.window
- chrome.storage
- chrome.contextMenus
- chrome.devtools
- chrome.extension
- 分类
- homepage_url 开发者或者插件主页
- 5种类型的JS对比及消息通信
- 其它补充
- 前端、移动端
- html5
- meta标签
- flex布局
- javascript
- 获取js对象所有方法
- dom加载
- ES6函数写法
- ES6中如何导入和导出模块
- 数组的 交集 差集 补集 并集
- bootstrap
- class速查
- 常见data属性
- 开源项目
- 会员 数据库表设计
- 程序执行
- 开发总结
- API接口
- API接口设计
- json转化
- app接口