This article mainly introduces the PHP implementation of the basis of AOP, has a certain reference value, now share to everyone, the need for friends can refer to
AOP is the continuation of OOP, is the abbreviation of Aspect oriented programming, meaning aspect-oriented programming, and some call it aspect-oriented programming. Aspect-oriented programming is useful in enterprise-level development. For example, before we invoke certain methods, we need to make a log record, and after calling the method we need a log record. Let's take a look at not using AOP, how we do it in PHP.
<?php/** * Log class * /class Systemlog { function Beforelog () { //write before log } function Afterlog () { //write after log }}/** * News class * /Class News extends Systemlog { function Add () { $this->befo ReLog (); Add News $this->afterlog (); } function Delete () { //delete news }}
We define a log class that contains two methods Beforelog () and Afterlog (), which record the pre-and post-call logs, then we define the news class, the news class inherits the log class, and to complete the logging before and after the call to the Add () method, we only have the add () Method internally calls Beforelog () and Afterlog () two methods. While we implement logging before/after calling the Add () method, this code is not elegant in OOP programming, and it violates the idea of "weak coupling, strong cohesion," because the Add () method under the news class is just the thing that should be added, and then we're in Add () The log is also recorded in the method, and if we need to add logging to many methods, we will call many times the Beforelog () and Afterlog () methods, bringing a lot of duplicate code.
In PHP5, if an undefined method is called, the _call () method is called automatically. With this feature of PHP5, we can implement AOP programming in the _call () approach, which is an AOP prototype that is not as powerful as Java.
<?php/** * Log class */class Systemlog { function _call ($method, $args) { $method = "_$method";//Add ' _ ' before the method name to be called , $method the name of the method to invoke //write before log $return = Call_user_func_array (Array ($this, $method), $args); Write after log return $return;} }/** * News class * /Class News extends Systemlog { function _add () { Add News } function Delete () { //delete news }}
News class or inherit the log class, it should be noted that we added ' _ ' before the Add () method, and there is no logging related code in the Add () method.
$news = new News (); $news->add ();
Call the Add () method in the news class, noting that the overridden news class does not have the Add () method, only the _add () method, when PHP cannot find the Add () method automatically calls the _call () method, the _call () method has two parameters, The first parameter is the name of the called method, the second parameter is the parameter of the called method, in the _call () method first adds ' _ ' before the called method name, where the method after adding ' _ ' becomes the _add,_add () method exists in the news class, and then the Write method is called before the log , and then use the Call_user_func_array () function to execute the previous object method call, and the method call completes the Write method call to the Dogo Katsuragi log.
In retrospect, we implemented AOP by using programming conventions and calling undefined methods in PHP5 to automatically invoke the _call () method, but the AOP is still rudimentary and not powerful enough, and with the development of PHP, I believe the future of PHP will be more powerful in terms of AOP.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!