Home Backend Development PHP Tutorial Thoughts after reading 'In-depth PHP Object Orientation, Patterns and Practice' (3)

Thoughts after reading 'In-depth PHP Object Orientation, Patterns and Practice' (3)

Jul 29, 2016 am 09:00 AM
class function quot

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.

Thoughts after reading In-depth PHP Object Orientation, Patterns and Practice (3)

<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>
Copy after login

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.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? Aug 26, 2023 pm 10:58 PM

Vue error: Unable to use v-bind to bind class and style correctly, how to solve it? In Vue development, we often use the v-bind instruction to dynamically bind class and style, but sometimes we may encounter some problems, such as being unable to correctly use v-bind to bind class and style. In this article, I will explain the cause of this problem and provide you with a solution. First, let’s understand the v-bind directive. v-bind is used to bind V

How to determine whether an element has a class in jquery How to determine whether an element has a class in jquery Mar 21, 2023 am 10:47 AM

How jquery determines whether an element has a class: 1. Determine whether an element has a certain class through the "hasClass('classname')" method; 2. Determine whether an element has a certain class through the "is('.classname')" method.

How to solve the '[Vue warn]: v-bind:class/ :class' error How to solve the '[Vue warn]: v-bind:class/ :class' error Aug 26, 2023 am 08:17 AM

How to solve the "[Vuewarn]:v-bind:class/:class" error During the development process of using Vue, we often encounter some error prompts. One of the common errors is "[Vuewarn]:v-bind:class" /:class" error. This error message usually appears when we use v-bind:class or :class attribute, indicating that Vue cannot correctly parse the class value we set. Then, if

See all articles