Table of Contents
Study notes---Dahua PHP design pattern
Home Backend Development PHP Tutorial Study Notes---Dahua PHP Design Pattern_PHP Tutorial

Study Notes---Dahua PHP Design Pattern_PHP Tutorial

Jul 12, 2016 am 09:07 AM
notes Design Patterns

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 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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

The difference between design patterns and architectural patterns in Java framework The difference between design patterns and architectural patterns in Java framework Jun 02, 2024 pm 12:59 PM

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

Analysis of the Decorator Pattern in Java Design Patterns Analysis of the Decorator Pattern in Java Design Patterns May 09, 2024 pm 03:12 PM

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

The wonderful use of the adapter pattern in Java design patterns The wonderful use of the adapter pattern in Java design patterns May 09, 2024 pm 12:54 PM

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

PHP design pattern practical case analysis PHP design pattern practical case analysis May 08, 2024 am 08:09 AM

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

PHP Design Patterns: Test Driven Development in Practice PHP Design Patterns: Test Driven Development in Practice Jun 03, 2024 pm 02:14 PM

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

How design patterns deal with code maintenance challenges How design patterns deal with code maintenance challenges May 09, 2024 pm 12:45 PM

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

What are the advantages and disadvantages of using design patterns in java framework? What are the advantages and disadvantages of using design patterns in java framework? Jun 01, 2024 pm 02:13 PM

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

Application of design patterns in Guice framework Application of design patterns in Guice framework Jun 02, 2024 pm 10:49 PM

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

See all articles