search
HomeBackend DevelopmentPHP TutorialStudy Notes---Dahua PHP Design Pattern_PHP Tutorial

Study notes---Dahua PHP design pattern

PHPStorm IDE
Selection of development fonts: Source Code Pro, Courier New, Concolas
php namespace: can resolve conflicts between methods with the same name in different classes
namespace test1;
function test(){
Auto loading:
function __autoload($class){
require __DIR__.'/'$class.'.php';
}
spl_autoload_register(); This function can allow multiple autoloads and must be in the first line
For example:
spl_autoload_register('autoload1');
spl_autoload_register('autoload2');
function autoload1($class){
require __DIR__.'/'$class.'.php';
}
function autoload2($class){
require __DIR__.'/'$class.'.php';
}
require later class name::method name call();
For example: Test1::test();Test2::test();
PSR-0 Specification
1 The namespace of php must be consistent with the absolute path
2 The first letter of the class name and the file name must be capitalized
3 Except for intersection files, other ".php" must have only one class and cannot have executable code. For example: echo();
__DIR__ Magic constant Get the path of the current file
__FUNCTION__ Magic constant Get the current method
__METHOD__ Magic constant to get the current class name and method
BASEDIR Website root directory
SPL standard introduction
SplStack(); first-in-last-out stationary data structure
$stack=new SplStack();
$stack->push("test1"); //First inbound
$stack->push("test2"); //Advanced
echo $stack->pop(); //Last exit outbound test2
echo $stack->pop(); //pop out test1
SplQueue(); first-in-first-out queue data structure
$SplQueue=new SplQueue();
$SplQueue->enqueue("test1"); //Advanced
$SplQueue->enqueue("test2"); //Advanced
echo $SplQueue->dequeue(); //First out test1
echo $SplQueue->dequeue(); //First out test2
SplMinheap(); Heap data structure
$SplMinheap=new SplMinheap();
$SplMinheap->insert('test1');
$SplMinheap->insert('test2');
echo $SplMinheap->extract(); test1
echo $SplMinheap->extract(); test2
$sqlFixedArray(); Fixed size array (fixed length)
$array= new $sqlFixedArray(10); (set the data length in brackets)
$array['0']=1;
$array['8']=8;
PHP chain test operation implementation The advantage is that you can use one line of code to implement many functions
$ob->where()->order()->limit()->select();
PHP advanced object-oriented features
Using PHP magic methods
1 __get/__set                                                                                                                                                                                      
2 __call/__callStatic //Used to control PHP method calls/control class static method calls
3 __toString //Convert a PHP object into a string
4 __invoke                                                                                                                                                                                  
The printed result will be printed regardless of whether the subscripts 1-7 have a value. If there is no value, a null value will be automatically given
PHPstorm configuration and installation
http://blog.csdn.net/ikscher/article/details/43672365
Three basic design patterns
Factory pattern: Use factory methods or classes to generate objects, instead of directly replacing new with new in the code
Benefits: In case of changes, you only need to change one factory pattern class
Singleton mode: allows objects of a certain class to be created only once
Benefits: No matter how many instance objects are needed, the database is actually only connected once
Registration mode: Global shared exchange object works better with factory mode
Benefit: globally shared exchange object
Trying the registration tree together with the factory mode can reduce the waste of memory resources. Because the same object is called. You can also add data pairs
Image mapping mode reduces direct operations on the database and operates the properties of the class library
Adapter Mode
Encapsulate different interfaces into a unified API
PHP classes are single inheritance, that is, multiple inheritance is not supported. When a class requires the functions of multiple classes, inheritance is powerless. For this reason, PHP cited
Incorporated class interface technology.
If all methods in an abstract class are abstract methods, no variables are declared, and all members in the interface are public
Permissions, then this special abstract class is called interface.
The interface is defined using the keyword interface, and the keyword implements is used to implement the methods in the interface, which must be fully implemented.
Strategy Pattern Encapsulates a specific set of behaviors and algorithms into classes to adapt to certain specific contexts. This pattern is the Strategy Pattern
For example, judging the gender of the user and giving different needs
Write the man’s needs first and then write the woman’s needs
Decide which requirement to call based on the transmitted value, instead of using cumbersome judgment to implement it. If you want to change it, just write another requirement
Data object mapping mode
1 Data object mapping mode is to map objects and data storage. Operations on an object will be mapped to operations on data storage
Observer Pattern When the state of an object changes, all objects that depend on it will be notified and automatically updated. Useful When an event occurs
When it is generated, everything related to it can be written in the observer class
Prototype mode is similar to factory mode, both are used to create objects
Different from the implementation of the factory pattern, the prototype pattern creates a prototype object in advance and then creates a new object by cloning the prototype object.
This eliminates the need for initialization when creating a class
Prototype pattern is suitable for the creation of large objects. Creating a large object requires a lot of overhead. If it is new every time, it will consume a lot of money. The prototype mode is only
Memory copy is required.
Use: For example, a class object created involves many loops and the like. After new is created, the object can be cloned directly the next time it is called
Instead of continuing with new
Decorator mode Decorator mode (Decorator) can dynamically add and modify the function of a class. A class provides a function. If you modify
And adds additional functionality to the traditional editing mode, you need to write a subclass to inherit it and reimplement the class method
Using the decorator pattern, you only need to add a decorator object at runtime to achieve maximum flexibility.
Iterator pattern traverses the internal elements of an aggregate object without knowing the internal implementation
Compared with traditional programming patterns, the iterator pattern can hide the operations required to traverse elements
namespace test;
class AllUser implements Iterator {//Call the iterator interface provided by PHP itself
protected $ids;
protected $data = array();
protected $num ;
function __construct(){
//Instantiate the model
//Read database
}
function valid(){//Verify whether there is another element currently
}
function next(){//Get the next element
}
function current(){ //Get the current element
}
function rewind(){//Reset the entire iterator
}
function key(){//Get the position in the iterator
}
Agent Mode
Establish a proxy object (proxy) between the client and the entity. The client delegates all operations on the entity to the proxy object, hiding the entity
Specific implementation details of
The proxy can also be separated from the business code and deployed to another server. The business code delegates tasks through RPC.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1060602.htmlTechArticleStudy notes---Dahua PHP design pattern PHPStorm IDE development font selection: Source Code Pro, Courier New, Concolas PHP namespace: can solve the conflict of methods with the same name in different classes...
Statement
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
如何在PHP后端功能开发中合理应用设计模式?如何在PHP后端功能开发中合理应用设计模式?Aug 07, 2023 am 10:34 AM

如何在PHP后端功能开发中合理应用设计模式?设计模式是一种经过实践证明的解决特定问题的方案模板,可以用于构建可复用的代码,在开发过程中提高可维护性和可扩展性。在PHP后端功能开发中,合理应用设计模式可以帮助我们更好地组织和管理代码,提高代码质量和开发效率。本文将介绍常用的设计模式,并给出相应的PHP代码示例。单例模式(Singleton)单例模式适用于需要保

如何通过编写代码来学习和运用 PHP8 的设计模式如何通过编写代码来学习和运用 PHP8 的设计模式Sep 12, 2023 pm 02:42 PM

如何通过编写代码来学习和运用PHP8的设计模式设计模式是软件开发中常用的解决问题的方法论,它可以提高代码的可扩展性、可维护性和重用性。而PHP8作为最新版的PHP语言,也引入了许多新特性和改进,提供更多的工具和功能来支持设计模式的实现。本文将介绍一些常见的设计模式,并通过编写代码来演示在PHP8中如何运用这些设计模式。让我们开始吧!一、单例模式(Sing

深入聊聊设计模式利器之“职责链模式”(附go实现流程)深入聊聊设计模式利器之“职责链模式”(附go实现流程)Jan 17, 2023 am 11:43 AM

本篇文章给大家带来了关于golang设计模式的相关知识,其中主要介绍了职责链模式是什么及其作用价值,还有职责链Go代码的具体实现方法,下面一起来看一下,希望对需要的朋友有所帮助。

Go语言中的ETL的设计模式Go语言中的ETL的设计模式Jun 01, 2023 pm 09:01 PM

随着数据的增长和复杂性的不断提升,ETL(Extract、Transform、Load)已成为数据处理中的重要环节。而Go语言作为一门高效、轻量的编程语言,越来越受到人们的热捧。本文将介绍Go语言中常用的ETL设计模式,以帮助读者更好地进行数据处理。一、Extractor设计模式Extractor是指从源数据中提取数据的组件,常见的有文件读取、数据库读取、A

深入解析Go语言中的单例模式深入解析Go语言中的单例模式Mar 21, 2023 pm 06:36 PM

单例模式是一种常见的设计模式,它在系统中仅允许创建一个实例来控制对某些资源的访问。在 Go 语言中,实现单例模式有多种方式,本篇文章将带你深入掌握 Go 语言中的单例模式实现。

了解JavaScript中的设计模式和最佳实践了解JavaScript中的设计模式和最佳实践Nov 03, 2023 am 08:58 AM

随着JavaScript的不断发展和应用范围的扩大,越来越多的开发人员开始意识到设计模式和最佳实践的重要性。设计模式是一种被证明在某些情况下有用的软件设计解决方案。而最佳实践则是指在编程过程中,我们可以应用的一些最佳的规范和方法。在本文中,我们将探讨JavaScript中的设计模式和最佳实践,并提供一些具体的代码示例。让我们开始吧!一、JavaScript中

设计模式的六大原则是什么设计模式的六大原则是什么Jan 06, 2023 pm 04:25 PM

设计模式的六大原则:1、单一职责原则,其核心就是控制类的粒度大小、将对象解耦、提高其内聚性;2、开闭原则,可以通过“抽象约束、封装变化”来实现;3、里氏替换原则,主要阐述了有关继承的一些原则;4、依赖倒置原则,降低了客户与实现模块之间的耦合;5、接口隔离原则,是为了约束接口、降低类对接口的依赖性;6、迪米特法则,要求限制软件实体之间通信的宽度和深度。

探索Java开发中的设计模式经验与建议探索Java开发中的设计模式经验与建议Nov 22, 2023 pm 04:08 PM

探索Java开发中的设计模式经验与建议设计模式是软件开发中用于解决特定问题的一种面向对象的可复用解决方案。在Java开发中,设计模式是很重要的一部分,它能够提高代码的可读性和可维护性,并且能够加速开发过程。通过运用设计模式,开发人员可以更好地组织和管理代码,同时也能够避免一些常见的开发错误。在Java开发中,有很多常用的设计模式,如单例模式、工厂模式、观察者

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor