Home > Article > Backend Development > ThinkPHP3.0 Understanding_PHP Tutorial
I am very excited to see the new version of ThinkPHP 3.0, because it proposes many new concepts, such as CBD mode and AOP programming ideas. Focus on summarizing these two new concepts.
CBD knowledge
Quoting the official manual: ThinkPHP version 3.0 introduces a new CBD (Core + Behavior + Driver) architecture model, because from the bottom, the framework adopts the core + behavior + driver architecture system, and the core retains the most critical parts, and Labels are set at important positions for marking, and other functions are combined using behavioral extensions and drivers. Developers can extend or replace a certain label position according to their own needs, and can easily customize the bottom layer of the framework. You can add your own label positions and add application rows in the application layer. The label position is similar to the "aspect" in the AOP concept, and the behaviors are programmed around this "aspect". If the system's built-in core extensions are regarded as a standard mode, then users can customize all these behaviors. Packaged into a new pattern, so in ThinkPHP, it is called pattern extension. In fact, pattern extension can not only replace and add behaviors, but also replace and modify the underlying MVC to achieve customized purposes. Using this new feature, developers can easily customize a development framework for themselves or their companies through pattern extensions. The new version of pattern extensions is the master of framework extensions and creates a new milestone. This is exactly That’s the real beauty of the new version.
AOP recognition
Quote from the official manual:
AOP (Aspect-Oriented Programming, aspect-oriented programming) can be said to be the supplement and improvement of OOP (Object-Oriented Programming, object-oriented programming). OOP introduces concepts such as encapsulation, inheritance, and polymorphism to establish an object hierarchy to simulate a collection of public behaviors. When we need to introduce public behavior to dispersed objects, OOP is powerless. In other words, OOP allows you to define relationships from top to bottom, but it is not suitable for defining relationships from left to right. For example, the logging function. Logging code tends to be spread horizontally across all object hierarchies, having nothing to do with the core functionality of the objects it's spread to. The same is true for other types of code, such as security, exception handling, and transparent persistence. This kind of irrelevant code scattered everywhere is called cross-cutting code. In OOP design, it leads to the duplication of a large amount of code and is not conducive to the reuse of various modules. AOP technology is just the opposite. It uses a technology called "cross-cutting" to dissect the inside of the encapsulated object and encapsulate the public behaviors that affect multiple classes into a reusable module and name it. is "Aspect", that is, aspect. The so-called "aspect", simply put, is to encapsulate the logic or responsibilities that have nothing to do with the business but are jointly called by the business modules, so as to reduce the duplication of code in the system, reduce the coupling between modules, and facilitate future reliability. Operability and maintainability. AOP represents a horizontal relationship. If the "object" is a hollow cylinder, which encapsulates the properties and behavior of the object; then the aspect-oriented programming method is like a sharp knife, cutting these hollow cylinders into pieces. Open it to get its internal information. The cut sections are the so-called "aspects". Then it restored these cut sections with uncanny skills, leaving no trace.
Using "cross-cutting" technology, AOP divides the software system into two parts: core concerns and cross-cutting concerns. The main process of business processing is the core concern, and the part that has little relationship with it is the cross-cutting concern. One characteristic of cross-cutting concerns is that they often occur in multiple places in the core concern, but are basically similar everywhere. Such as authority authentication, logging, and transaction processing. The role of Aop is to separate various concerns in the system, separating core concerns and cross-cutting concerns. As Adam Magee, senior solution architect at Avanade, said, the core idea of AOP is to "separate the business logic in the application from the common services that support it."
Personal understanding: AOP is a programming idea, which is to make some functional modules that need to be reused throughout the project into aspects one by one, so that we will not write code repeatedly in this project. CBD is the implementation of AOP thinking. Some functions that need to be used (the so-called aspects) are written into Behaviors one by one. In this way, we only need to use this behavior. There is only one method that can be called for this behavior. run, the behaviors of other Behavior classes are private attributes. Moreover, this calling method is encapsulated into the method B($name, &$param). When we call the aspect behavior, we only need to execute the B method. (This form is very consistent with the idea of dependency injection, and the advantage is to achieve code decoupling).
So, if you use ThinkPHP2.X, how can you make better use of these good programming ideas without changing the framework? Before starting a project, you must first make an overall plan for the project. For example, for some recurring behaviors, such as project-level special verification of data, you can make it into a aspect (Behavior), so that we only need to reuse this section.
I have probably read ThinkPHP 3.0, and I personally feel that there is not much difference between 3.0 and 2. Take advantage of the nutrients and use them in your daily programming