Home > Article > Backend Development > Thoughts after reading 'In-depth PHP Object Orientation, Patterns and Practice' (3)
Command pattern:
When I first came into contact with this command pattern, it felt like a simplified mvc framework. Access from different paths, and then the controller determines the specific php file to be called.
<span><span><?php </span><span><em></em></span><span>class </span><span>CommandContext{ </span><span><em>//"</em></span><span><em>命令容器</em></span><span><em>" </em></span><span><em></em></span><span>private </span><span>$params </span><span>= </span><span>array</span><span>(); </span><span>function </span><span>addParam(</span><span>$key</span><span>,</span><span>$val</span><span>){ </span><span>$this</span><span>-></span><span>params</span><span>[</span><span>$key</span><span>] = </span><span>$val</span><span>; </span><span> } </span><span>function </span><span>getParam(</span><span>$key</span><span>){ </span><span>return </span><span>$this</span><span>-></span><span>params</span><span>[</span><span>$key</span><span>]; </span><span> } </span><span>} </span><span>class </span><span>Controller{ </span><span>private </span><span>$cmdContext</span><span>; </span><span>function </span><span>__construct(){ </span><span>$this</span><span>-></span><span>cmdContext </span><span>= </span><span>new </span><span>CommandContext(); </span><span> } </span><span><em>// </em></span><span><em></em></span><span>function </span><span>getCmdContext(){ </span><span>return </span><span>$this</span><span>-></span><span>cmdContext</span><span>; </span><span> } </span><span>function </span><span>process(){ </span><span>$action </span><span>= </span><span>$this</span><span>-></span><span>cmdContext</span><span>->getParam(</span><span>"action"</span><span>); </span><span><em>//</em></span><span><em>通过</em></span><span><em>“</em></span><span><em>命令容器</em></span><span><em>”</em></span><span><em>获得命令 </em></span><span><em></em></span><span>$command </span><span>= CommandFactory::</span><span><em>getCommand</em></span><span>(</span><span>$action</span><span>); </span><span><em>//</em></span><span><em>将</em></span><span><em>“</em></span><span><em>命令</em></span><span><em>”</em></span><span><em>传给</em></span><span><em>“</em></span><span><em>命令工厂</em></span><span><em>”</em></span><span><em>,得到</em></span><span><em>“</em></span><span><em>命令</em></span><span><em>”</em></span><span><em>所对应的子</em></span><span><em>command</em></span><span><em>类对象 </em></span><span><em></em></span><span>if</span><span>(</span><span>$command</span><span>->execute(</span><span>$this</span><span>-></span><span>cmdContext</span><span>)){</span><span><em>//</em></span><span><em>调用子类对象的</em></span><span><em>execute</em></span><span><em>方法并判断 </em></span><span><em></em></span><span><em>//</em></span><span><em>成功 </em></span><span><em></em></span><span><em>//</em></span><span><em>调用对应视图 </em></span><span><em></em></span><span>}</span><span>else</span><span>{ </span><span><em>//</em></span><span><em>失败 </em></span><span><em></em></span><span>} </span><span> } </span><span>} </span><span>class </span><span>CommandFactory{ </span><span>static function </span><span>getCommand(</span><span>$cmd</span><span>){ </span><span>$file </span><span>= </span><span>'commands/'</span><span>.</span><span>$cmd</span><span>.</span><span>'Command.php'</span><span>; </span><span><em>//</em></span><span><em>命令所对应的</em></span><span><em>php</em></span><span><em>文件路径 </em></span><span><em></em></span><span><em></em></span><span>if</span><span>(! </span><span><em>file_exists</em></span><span>(</span><span>$file</span><span>)){ </span><span>throw new </span><span>Exception(</span><span>"Could not find file </span><span>$file</span><span>"</span><span>); </span><span> } </span><span>require_once</span><span>(</span><span>$file</span><span>); </span><span>$class </span><span>= </span><span>$cmd</span><span>.</span><span>'Command'</span><span>; </span><span><em>//</em></span><span><em>形成类名 </em></span><span><em></em></span><span>if</span><span>(! </span><span><em>class_exists</em></span><span>(</span><span>$class</span><span>)){ </span><span>throw new </span><span>Exception(</span><span>"Could not find class </span><span>$class</span><span>"</span><span>); </span><span> } </span><span>$result </span><span>= </span><span>new </span><span>$class</span><span>(); </span><span>return </span><span>$result</span><span>; </span><span> } </span><span>} </span><span><em>//commands</em></span><span><em>文件夹内 </em></span><span>abstract class </span><span>Command{ </span><span>abstract function </span><span>execute(CommandContext </span><span>$commandContext</span><span>); </span><span>} </span><span>class </span><span>demoCommand </span><span>extends </span><span>Command{ </span><span>function </span><span>execute(CommandContext </span><span>$commandContext</span><span>){ </span><span>return </span><span>"ok"</span><span>; </span><span> } </span><span>} </span><span><em>//</em></span><span><em>使用代码 </em></span><span>$controller </span><span>= </span><span>new </span><span>Controller(); </span><span>$cmdContext </span><span>= </span><span>$controller</span><span>->getCmdContext(); </span><span>$cmdContext</span><span>->addParam(</span><span>"action"</span><span>,</span><span>"demo"</span><span>); </span><span>$demo </span><span>= </span><span>$controller</span><span>->process(); </span><span>?></span></span></span>
The above introduces my thoughts after reading "In-depth PHP Object-Oriented, Patterns and Practices" (Part 3), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.