PHP Notes: The Application of AOP _php tutorial

Source: Internet
Author: User
Introduced

Have you ever heard of AOP (Aspect oriented programming)? Although there seems to be no excessive use in PHP, AOP is widely used in enterprise-level development. I'll take this article to introduce you to the AOP aspects of PHP.

This article mainly explains the concept of AOP.


What is AOP?

In application development, we often find that a lot of functionality needs to be scattered over multiple points in the code, but these points are virtually unrelated to the actual business. For example, before performing some special tasks, we need to make sure that the user is in the login state, we call these special characters "cross-cutting concerns", let us through Wikipedia to understand "cross-cutting concerns" Definition of (horizontal relationship).
In computer science, "cross-cutting concerns" refers to "tangent (or direction) programming." These relationships cannot be broken down from other systems (framework design or some implementations) so that code duplication occurs, there is a meaningful dependency in the system, or both.
Now you should have a basic understanding of horizontal relationships, so let's see what they are in the code.

Imagine a scenario where you are an editor of a blog site. You need to log in to the site, then create posts, verify posts, edit posts, and more. If you do not log in, then you should go directly to the login screen. To ensure that these behaviors are secure, any of the above actions need to be validated effectively, as shown in the code below.
Copy CodeThe code is as follows:
Class Blogpost extends Ci_controller
{
Public Function Createpost () {
if (! Authentication::checkauthentication ()) {
Redirect to login
}
else {
Proceed
Messages::notifyadmin ();
}
}

Public Function Approvepost () {
if (! Authentication::checkauthentication ()) {
Redirect to login
}
else {
Proceed
}
}

Public Function Editpost () {
if (! Authentication::checkauthentication ()) {
Redirect to login
}
else {
Proceed
}
}

Public Function Viewpost () {
// ...
}
}

Looking at the code above, you will find that checkauthentication () is called before each method, because these actions require the user to log in before they can proceed. There is also the notifyadmin () to identify whether it is an administrator account in order to create a new post. See no, there are a lot of "duplicate code", and the blogpost class should only be responsible for managing posts. Authentication and identification should be separate. We violate the principle of "single responsibility".

The single responsibility principle tells that each class should have only a single responsibility (task), and that the entire responsibility should be encapsulated in a class. All services should be distributed in a balanced and rigorous way in accordance with their responsibilities.

So far, we have been able to understand the meaning of AOP. The cross-section relationship is put into a class, and we call this class "tangent." The process of separating the cross-sectional relationship from our core code is called Aspect oriented programming.

AOP Professional Terminology

There are many conditions that are specifically designed to explain the nature of AOP. Understanding these conditions will be the key to your successful integration of AOP into your project.
Aspect
Advice
Joinpoint
Pointcut
We have learned what the facets (Aspect) are! Now let's take a look at what the other three conditions mean.

Advice (notice)
Advice is used to invoke aspect (facets), as its name implies, advice is used to define what to do and when to do it. In our previous example, checkauthentication (what to Do) is advice (notification), which should be called before executing the code in the specified method.


joinpoint (Access point)
Joinpoint is where we create the advice app. Looking at the previous code, you will find that I have called several features that are not directly related to the business logic. In Createpost (), for example, cross-cutting concerns should occur before the validation logic is executed and after the message is sent to the administrator. These are probably the access points.

In your app's code, the access point can be placed in any location. But advice can only be arranged at certain points, depending on your AOP framework, which I'll discuss later.

Pointcut (Point cutting)
Point cutting defines a way to match notifications to certain access points. While there are only a couple of access points in our example, you can place thousands of access points in your app, and you don't need to apply notifications to all access points. You can bind some access points that you think are necessary to the notification.

Suppose we want to notify Createpost (), Approvepost (), and Editpost (), but now there is no viewpost (). We use some method to bind these three methods to the notification. We then create an XML file that contains the facets details that contain regular expressions that match the access point.

Summary: When a cross-cut relationship exists in our application, we can create a slice that applies the notification function on some access points that choose to use point cutting.


AOP Notification Type

Notice code we can behave in many ways. As I mentioned earlier, these notification codes depend on the framework you use, but there are some types you need to be familiar with, see below:
Prior notice
Notify after return
Notification after throwing
Perimeter notification

Prior Notice
Use notifications before you have some special points in your code-it's normal to call a method.

So far, in order to simplify the concept and to let you understand your code faster, I often write the notice into the method. But in the real world, notifications are often not written in the method. There should be a separate controller, each of which is in this controller, and each method is wrapped in the functionality of AOP. This global controller runs throughout the system and is invisible to us.
Copy CodeThe code is as follows:
Class Pathcontroller
{
function Controlpaths ($className, $funcName) {
Authentication::checkauthentication ();
$CLASSOBJ = new $className ();
$CLASSOBJ $funcName ();
}
}

Suppose there is a class here that is primarily intended to show you what actually happened to this class. Assuming that the Controlpaths method is a global pointcut in the application, accessing each method in the application needs to be accessed through this method. We called the Notification checkauthentication () before executing each method in the method above. -This is the previous notification.



Notify after return
This notification executes only once after the specified function has been executed and returns that access point. Consider the following code:
Copy the Code code as follows:
Class Pathcontroller
{
function Controlpaths ($className, $funcName) {
$CLASSOBJ = new $className ();
$CLASSOBJ $funcName ();
Database::closeconnection ();
}
}

Press CTRL + C to copy the code note here, when the method is complete, we clean up the database resources. After returning the notification, we call this notification.


Notification after throwing
If the function throws an exception during the execution of the process, the notification is applied after the exception is thrown. This is when the exception is thrown, and the notification becomes an error prompt.
Copy the Code code as follows:
Class Pathcontroller
{
function Controlpaths ($className, $funcName) {
try {
$CLASSOBJ = new $className ();
$CLASSOBJ $funcName ();
}
catch (Exception $e) {
Error::reporterror ();
}
}
}

Perimeter notification
The fourth kind of notice is peripheral notice, he is the merger of the notice and the notification after the return.
Copy the Code code as follows:
Class Pathcontroller
{
function Controlpaths ($className, $funcName) {
Logger::startlog ();
$CLASSOBJ = new $className ();
$CLASSOBJ $funcName ();
Logger::endlog ();
}
}

http://www.bkjia.com/PHPjc/326806.html www.bkjia.com true http://www.bkjia.com/PHPjc/326806.html techarticle introduction Have you ever heard of AOP (Aspect oriented programming)? Although there seems to be no excessive use in PHP, AOP is widely used in enterprise-level development. I will take this article ...

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