Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the core technologies of PHP

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the core technologies of PHP". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what the core technologies of PHP are.

I. the core concepts of object-oriented thought

1. The core idea of object-oriented thought is object, encapsulation, reusability and expansibility. Object-oriented is a higher-level abstraction based on process-oriented.

two。 Object orientation has nothing to do with the specific language.

a. The "shape" and "Ben" of Baidu object

1. A class is our description of a set of objects.

The ① class defines a series of properties and methods and provides actual operational details that can be used to process attributes.

The ② object contains the specific value of the class attribute, which is the instantiation of the class.

The relationship between ② class and object is like the relationship between service and being served, processing and being processed.

two。 Serialization means that all kinds of object states (attributes) stored in memory are saved and can be restored when needed.

3. The difference between an object and an array is that an object also has a pointer to the class to which it belongs

4. Objects and classes:

The ① class is a template that defines a series of properties and operations, while the object materializes the properties and then leaves them to the class to handle.

The ② object is data, and the object itself does not contain methods, but the object has a "pointer" to a class that can have methods.

The ③ method describes the differences caused by different attributes

The ④ class and the object are not separable. If there is an object, there must be a class corresponding to it, otherwise the object will become a child without relatives.

b. Application of magic method

1. Magic methods are special methods that start with two underscores and can be regarded as the "grammatical sugar" of PHP.

The overload of 2.php is different from that of Java, the constructor is not overloaded, while _ _ set and _ _ get are overloaded. Overloading of php refers to the dynamic "creation" of class properties and methods.

3. When an inaccessible method (such as undefined or invisible) is called, _ _ call () is called. In fact, magic methods make the dynamic creation of methods possible, which is a useful syntax in framework designs such as MVC.

c. Inheritance and polymorphism

1. Accessing a non-static method with "::" does not conform to the syntax, but PHP can still execute the code correctly. This is only a "compatibility" or "concession" of PHP. Modifying error_reporting to open e_strict will cause an error.

two。 Coupling is the measure of interconnecting programs between different modules in a software structure, that is, the dependency between different modules.

Low coupling means that modules exist independently between modules as much as possible, and the interfaces between modules are as few and simple as possible.

Prefer to use combinations because:

① inheritance destroys encapsulation

② inheritance is tightly coupled

③ inheritance extension complexity

Improper use of inheritance by ④ may violate real-world logic

3. Combination needs to create local objects one by one to increase the amount of code.

4. How to use inheritance:

① is specially designed for inherited parent classes. The inheritance tree is stable and no more than 3 layers.

② forbids inheritance of classes that are not specifically used for inheritance

③ gives priority to using combinatorial relationships to improve the reusability of code.

The ④ subclass is a special type, not just a role of the parent class

⑤ subclass extension, rather than overriding or invalidating the functionality of the parent class

The bottom code of ⑥ is multi-purpose combination, and the top / business layer code is multi-purpose inheritance.

5. Polymorphism: objects of the same class will get different results when they receive the same message, and this message is unpredictable, as the name implies, multiple states, multiple results.

6. The real meaning of polymorphism is that in actual development, as long as you care about the programming of an interface or base class, you don't have to care about the specific class to which an object belongs.

7.PHP is weakly typed and has no object conversion mechanism, so you can't assign a derived class object to a base class object (calss a = new class b) like C++ or Java, so the parent class overload of PHP is not polymorphic. The interface implementation of PHP is polymorphic.

8. The key to distinguishing whether an object is polymorphic or not is whether the object is of the same type. The essence of polymorphism is if...else, but at different levels of implementation.

d. Interface-oriented programming

1. The interface defines a set of specifications that describe the functions of a "thing" and require that these basic functions must be implemented if the real "thing" is to be available. The interface describes itself like this: "for all the classes that implement me, it should look like what I am now." interfaces are created for abstraction. Therefore, in the program, the methods of the interface must be fully implemented, otherwise a fetal error will be reported.

two。 The interface regulates not only the implementer of the interface, but also the executor of the interface, which is not allowed to call methods that do not exist in the interface. Of course, this is not to say that if a class implements an interface, it can only implement methods that are only available in the interface, but that if it is aimed at an interface, not a specific class, it can only act according to the agreement of the interface.

The interface of 3.PHP is insufficient in contract-oriented programming, which can be diluted into design documents and play the role of a team's basic contract. Because PHP is weakly typed and emphasizes flexibility, it is not recommended to use interfaces on a large scale, but only in some "kernel" code.

4. The interface itself does nothing, and the system quietly implements the behavior of the interface internally. Traits can be seen as an enhanced interface.

e. Reflection

1. Intuitive understanding is to find the departure and source according to the place of arrival. In the PHP running state, expand the analysis PHP program, export or extract the details of half-day classes, methods, properties, etc., including comments. This ability to dynamically obtain information and dynamically call object methods is called reflection.

two。 Reflection can be used not only for classes and objects, but also for functions, extension modules, exceptions, etc.

3. Use a reflection: one is to debug the object, and the other is to get the information about the class. Reflection is very expensive, so don't abuse it when an alternative can be found.

4. In many cases, making good use of reflection can keep the code elegant and concise, but reflection can also destroy the encapsulation of the class, because reflection can force the exposure of methods or properties that should not have been exposed, which is both an advantage and a disadvantage.

f. Exception and error handling

The exception in 1.PHP is the situation that does not conform to expectations in the movement of the program and is different from the normal process. An abnormal situation, in which normal logic should not go wrong, but still goes wrong, is an interruption of logic and business processes, not a language error. Errors in PHP are their own problems, which are caused by illegal syntax or environmental problems that prevent the compiler from passing the check or even running.

two。 In PHP, any self-error triggers an error instead of throwing an exception (in some cases, both an error and an exception are thrown). When PHP encounters abnormal code, it usually triggers an error instead of throwing an exception. In this sense, it is impossible to use exceptions to handle unexpected problems. For example, it is not feasible to trigger an exception when the file does not exist and the database link cannot be opened, which is thrown as an error in PHP rather than automatically caught as an exception.

3.PHP usually cannot catch meaningful exceptions, it treats all anomalies as errors, and if you want to catch this exception, you have to use the if...else structure.

4. When to use the exception handling mechanism:

① 's pessimistic prediction of the program

② program needs and business concerns: exception handling mechanisms can consider everything as a transaction and exceptions as a built-in recovery system

Robustness requirements at the ③ language level: the advantage of try..catch is that it can minimize 10 million abnormal logic interruptions, and does not affect the integrity of business logic after remedial processing; throwing exceptions and only throwing but not catching, or capturing without remediation will lead to data confusion.

A 5.PHP error is a condition that causes the script to run abnormally, the error level:

① deprecated: means "not recommended, not recommended"

② notice: there are inadequacies in grammar

③ warning: modify the code when there is a very inappropriate situation in the syntax, such as a mismatch of function parameters.

④ fetal error: fatal error, which directly leads to the termination of the php process. The following code is not executed and must be modified.

⑤ prase error: syntax parsing error, causing PHP code to fail syntax checking

Error handling in 6.PHP:

① set_error_handler () sets a user-defined error handling function. If you use this function, the error handling function of php will be bypassed. You can use restore_error_handler () to cancel the takeover.

The ② trigger_error function can throw an error

II. Principles of object-oriented design

a. Single responsibility principle (SRP): as far as a class is concerned, there should be only one reason for its change. It is the problem of how to design the class and the method definition of the class.

One is to avoid spreading the same responsibilities into different classes, and the other is to prevent one class from taking on too many responsibilities.

Why comply with SRP?

① can reduce the coupling between classes

② improves the reusability of classes

two。 Factory mode: instantiates objects during code execution, is responsible for producing objects, and generates different instantiated objects according to different parameters.

3. Command mode: separates the command requestor from the command implementer.

4. It has always been a simple practice to follow:

① abstracts the business objects according to the business process.

Attention should be paid to the classification of ② responsibilities.

b. Interface isolation principle (ISP): if a module contains multiple submodules, then we should be careful to abstract the module; indicating that clients should not be forced to implement interfaces they will not use, should group the methods in the fat interfaces, and then replace it with multiple interfaces, each serving a submodule, in short, it is much better to use multiple specialized interfaces than a single interface.

1. Main points:

① the dependency of one class on another should be based on the smallest interface. ISP can achieve methods that do not force customers to rely on them; ISP can also reduce customer interaction.

The ② client program should not rely on interface methods (functions) that it does not need

What 2.ISP emphasizes is that the less commitment the interface has to the client, the better, and to be single-minded.

3. Interface contamination: too bloated interface design is the pollution of the interface. Is to add unnecessary responsibilities to the interface, if the main purpose of the developer to add a new feature to the interface is to reduce the number of interface implementation classes, then this design will cause the interface to be constantly "contaminated" and "fat". "Interface isolation" is actually the principle of customized service design.

Treatment method:

① uses delegation to separate interfaces

② uses multiple inheritance to separate interfaces

c. Open-closed principle:

1. Definition:

Open: the behavior of the module must be open and extensible, not rigid

Closed: when extending the function of a module, it should not affect or affect existing program modules on a large scale.

two。 In other words, developers are required to expand the software functions of the application system without modifying the existing functional code in the system; a module should be open in expansibility and closed in modification.

3. How to follow the open-closed principle: the core idea is to program abstractions rather than concrete ones, because abstractions are relatively stable. The modification is closed by making the class rely on a fixed abstraction; through object-oriented inheritance and polymorphism mechanism, we can inherit the abstract body, change its inherent behavior and implement new extension methods by overriding its methods. so it's open to extension.

① fully applies the ideas of "abstraction" and "encapsulation" in design.

② applies interface-oriented programming in system function programming.

d. Substitution principle (LSP, Richter substitution principle): subclasses must be able to replace their base classes

1. Definition: subtypes must be able to replace their parent types and appear wherever the parent class can appear. If a software nudity uses a base class, it must be applicable to its subclass, and it is impossible to detect the difference between the base class object and the subclass object.

two。 How to comply with:

The methods of the ① parent class are implemented or overridden in the subclass, and the derived class only implements the methods declared in its abstract class, and should not give redundant method definitions or implementations.

② should only use parent class objects in client programs, not subclass objects directly, so that runtime binding can be implemented.

3.PHP 's support for LSP is not friendly and lacks the concept of upward transformation, which can only be achieved through some tortuous methods.

e. Dependency inversion principle: to invert the dependency relationship to the dependency interface

1. Definition:

The upper module of ① should not depend on the lower module, they all depend on an abstraction.

② abstraction cannot depend on concrete, which should depend on abstraction.

two。 Abstractions are generally relatively stable or relatively infrequent, while the details are volatile.

3.IOC is synonymous with the principle of dependency inversion. Dependency injection (DI) and dependency lookup (DS) are two implementations of IOC.

4.PHP doesn't have a complete IOC container, or PHP doesn't need it.

5. How to satisfy IOC:

Each high-level class proposes an interface declaration for the service it needs, and the lower-level class implements this interface.

Each higher-level class uses the service through this abstract interface

f. Some are superior to process-oriented

1. It is no longer difficult to join and integrate new members.

two。 The code is the document

3. We should not only have an in-depth understanding of the idea of object-oriented, but also not cling to it.

III. The basis and application of regular expressions

a. Recognize regular expressions

There are two sets of regular functions in 1.PHP:

① is a function provided by the PCRE library, preceded by "preg_".

② is a function provided by the POSIX extension, with the dropout name "ereg_" before, and is not recommended after PHP5.3.

two。 In PHP, a regular expression is divided into three parts: delimiters, expressions, and modifiers

IV. PHP Network Technology and its Application

Detailed explanation of A.HTTP protocol

1.HTTP is a communication specification based on the application layer: transferring hypertext from a WWW server to a local browser, consisting of requests and responses. Is a stateless protocol, and this request from the same client does not correspond to the last request. It is a typical question-and-answer interaction.

2.SPDY protocol optimizes the communication between browser and server, supports stream multiplexing, and has advanced features such as priority request, initiating request actively, forcing SSL secure transmission and so on.

3.HTTP workflow:

① client establishes a connection with the server

After the ② establishes the connection, the client sends a request to the server. The format is: uniform Resource Identifier (URL) in front, protocol version number in the middle, and MIME information (including request modifiers, client information, and possible content)

After receiving the request, the ③ server gives the corresponding response information. The format is: first a status line (including the protocol version number of the information, a success or error code), and then MIME information (including server information, physical information, and possible content)

The ④ client receives the information returned by the server and displays it on the user's screen, and then the client disconnects from the server.

4. Spam prevention measures

① IP restrictions

② verification code

③ Token and form spoofing

④ audit mechanism

b. Bag grabbing tool: Fiddler

C.Socket process Communication Mechanism and its Application

1.Scoket, often referred to as a "socket", is used to describe IP addresses and ports, and is a handle to a communication chain. The application sends a request to the network or answers the network request through the socket. Socket is neither a program nor a protocol, it is just a set of abstract API of the communication layer provided by the operating system.

The language features and self-positioning of 2.PHP determine that it is only suitable to be a client.

3. Note:

The first parameter of ① fsockopen, $hostname, does not take "http://"" unless you use SSL, etc.

② Headers requests are not always carried according to the packet capture data, unless the call is unsuccessful or unskilled or has special needs

③ has two line breaks after Connection and data

Some form requests of ④ may have hidden values

⑤ pay attention to the coding problem

D.cURL tools and applications

1. Basic steps for establishing a cURL request

① initialization

② setting options, including URL

③ executes and gets the contents of the HTML document

④ releases the cURL handle

2.curl_getinfo () is useful for debugging

e. Simple Mail transfer Protocol SMTP

1.SMTP is a set of rules for sending messages from source address to destination address, which are used to control the way letters are transferred.

The past Life and present Life of F.WebService

1.WebService

2.PHPRPC protocol

G.Cookie detailed explanation

The mechanism by which 1.Cookie stores data in a remote browser to track and identify users. In terms of implementation, Cookie is a small piece of data stored on the client, and the browser (that is, the client) interacts with the server through the HTTP protocol.

two。 Setting up Cookie with PHP is actually issuing commands for browsers to design Cookie.

3. Note:

The ① cookie function has a return value. If it is false, it means the setting failed, but it is for reference only.

The cookie set by ② on the current page by php cannot take effect immediately. You can't see it until the next page.

③ cookie does not have an explicit delete function

P3P setting is required for 4.cookie cross-domain

The cookie of a ① page cannot be the cookie of a browser process

When ② uses iframe, add some P3P information to the header of the corresponding dynamic page.

③ ie has strict restrictions on cross-domain access to cookie.

5. Local localStorage Stora

H.Session detailed explanation

1.session_set_save_handler () changes the way session is stored

2.sessionid is stored on the client as a cookie by default

Thank you for your reading, these are the contents of "what are the core technologies of PHP". After the study of this article, I believe you have a deeper understanding of what the core technologies of PHP have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report