Learning notes --- big talk PHP design pattern _ PHP Tutorial

Source: Internet
Author: User
Learning notes-Big Talk PHP design model. Learning notes --- big talk PHP design mode PHPStormIDE development font selection: SourceCodePro, CourierNew, Concolasphp namespace: can solve the same name in different classes of the Chong learning notes --- big talk PHP design mode
PHPStorm IDE

Development font selection: Source Code Pro, Courier New, Concolas

Php namespace: solves conflicts between methods with the same name in different classes

Namespace test1;

Function test (){

}

Automatic loading:

Function _ autoload ($ class ){

Require _ DIR _. '/' $ class. '. php ';

}

Spl_autoload_register (); this function allows multiple autoloads to be 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 ';

}

After require class name: method name call ();

Example: Test1: test (); Test2: test ();

PSR-0 specification

1. the php namespace must be consistent with the absolute path.

The first letter of the two types of names must be in uppercase.

3 except for intersection files, only one class of ". php" must have executable code such as echo ();

_ DIR _ magic constant to obtain the path of the current file

_ FUNCTION _ magic constant to obtain the current method

_ METHOD _ magic constant to get the current class name and METHOD

BASEDIR website root directory

Spl standard introduction

SplStack (); advanced post-site data structure

$ Stack = new SplStack ();

$ Stack-> push ("test1"); // advanced inbound

$ Stack-> push ("test2"); // advanced

Echo $ stack-> pop (); // post-exit test2

Echo $ stack-> pop (); // test1 is output later

SplQueue (); first-in-first-out queue data structure

$ SplQueue = new SplQueue ();

$ SplQueue-> enqueue ("test1"); // advanced

$ SplQueue-> enqueue ("test2"); // advanced

Echo $ SplQueue-> dequeue (); // output test1 first

Echo $ SplQueue-> dequeue (); // output test2 first

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

$ Array ['0'] = 1;

$ Array ['8'] = 8;

The advantage of PHP link trial is that a single line of code can be used to implement many functions.

$ Ob-> where ()-> order ()-> limit ()-> select ();

PHP object-oriented Advanced features

Use of PHP magic methods

1 _ get/_ set // take over the first letter of the object

2 _ call/_ callStatic // used to control PHP method calls/control class static method calls

3 _ toString // Convert a PHP object to a string

4 _ invoke // use PHP as a function

The printed result indicates that no value is printed whether the subscript is 1-7 or not, and a null value is automatically given.

PHPstorm configuration and installation

Http://blog.csdn.net/ikscher/article/details/43672365

Three basic design modes

Factory mode: factory methods or classes generate paired images, instead of directly replacing new in code

Advantage: only one factory mode class can be changed in case of a change.

Singleton mode: allows a class object to be created only once.

Benefit: no matter how many instance objects are needed, they actually only connect to the database once.

Registration mode: The global shared exchange object works better with the factory mode.

Benefit: global shared exchange objects

The registration tree and the factory model can be used together to reduce the waste of memory resources. Because the same object is called. You can also add a data pair

Similar to the ing mode, the attribute of the class library is used to reduce direct operations on the database.

Adapter mode

Encapsulate different interfaces into a unified API

PHP classes are single inheritance, that is, they do not support multi-inheritance. when a class requires multiple class functions, inheritance is powerless.

Added the class interface technology.

If all methods in an abstract class are abstract methods and no variables are declared, and all the members in the interface are public

Permission, so this special abstract class is called interface.

The interface is defined by the keyword interface, and the keyword implements is used to implement the methods in the interface, and must be fully implemented.

Policy mode encapsulates a group of specific behaviors and algorithms into classes to adapt to certain context environments. this mode is the policy mode.

For example, determine the different needs of users for men and women

Write down male's needs first and then write down female's needs

You only need to write another requirement to call that requirement based on the transmitted value instead of making the changes with tedious judgment.

Data object shooting mode

1. The data object ing mode maps objects and data storage. operations on an object are mapped to operations on data storage.

Observer mode when the state of an object changes, all objects dependent on it will be notified and automatically updated. When an event is triggered

All associated items can be written in the Observer class at birth.

Similar to the factory mode, the prototype mode is used to create objects.

Unlike the implementation of the factory mode, the prototype mode creates a prototype object in advance and then creates a new object by cloning the prototype object.

This removes the need for class initialization during class creation.

The prototype mode applies to the creation of large objects. Creating a large object requires a great deal of overhead. if a new object consumes a lot each time, the prototype mode is only

Memory copy is required.

Usage: for example, a created class object involves a lot of loops and so on. this object will be directly cloned when new is called next time.

Instead of continuing with new

The Decorator mode allows you to dynamically add and modify functions of a class.

And added additional features. in the traditional editing mode, you need to write a subclass to inherit it and re-implement the class method.

In the decorator mode, you only need to add a decorator object at runtime to achieve maximum flexibility.

The iterator mode traverses the internal elements of an aggregate object without having to know the internal implementation.

Compared with the traditional programming mode, the iterator mode 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 the database

}

Function valid () {// verify whether there is another element

}

Function next () {// Obtain the next element

}

Function current () {// Obtain the current element

}

Function rewind () {// reset the entire iterator

}

Function key () {// obtain the position in the iterator

}

}

Proxy mode

Create a proxy between the client and the object. the client delegates all operations on the object to the proxy object to hide the object.

Implementation details

The proxy can also be separated from the business code and deployed to another server. The service code delegates tasks through RPC.

PHPStorm IDE development font selection: Source Code Pro, Courier New, Concolas php namespace: can solve the same name methods in different classes punch...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.