A first look at AOP FOR PHP_PHP tutorial
Question
A first look at AOP FOR PHP
Solution
AOP is the continuation of OOP and is the abbreviation of Aspect Oriented Programming, which means aspect-oriented programming. AOP is actually a continuation of the GoF design pattern. The design pattern tirelessly pursues the decoupling between the caller and the callee. AOP can be said to be a realization of this goal. In fact, this technology appeared a long time ago. When I googled it, it was already a technology that appeared in 2006.
From my understanding, it is to cut the function horizontally without destroying the original method or class. Then add your own methods to deal with it. For example, we often have to perform permission judgment before executing some methods. After processing, log writing and other operations must be performed. The general operation method is to write the processing procedure at the head and bottom of the method. This destroys the single-function principle of OOP. Because when there are 100 or even 1000 methods to perform the same processing, it is inevitable that some unnecessary errors will occur. This is AOP in action. . .
When we write a function to output some information, we don't want people who don't have permission to see the information to see it before processing. Some common cache information may be written after processing. The usual way to write it is like this class OneTest{
public function getInfo(){
//Check the permissions
ACL::checkRole();
//Do the action process of obtaining information;
.....
//Write to cache
Cache::writeCache();
}
}
Copy the code What if there are 1000 methods that need to do the same operation. . Modify one place and modify everywhere. Difficult maintenance situations arise. To use AOP to deal with this problem, you only need this class OneTest{
public function getInfo(){
//Do the action process of obtaining information;
.....
}
}
Copy the code so that the two that break the encapsulation are taken out and defined in other places. . . . . That's roughly what it means. Of course, the misunderstanding was a mistake and it was purely a coincidence.
You can google for detailed introduction. . Some information has not been sorted out yet. . . . . This was done in my spare time while busy.
Here are a few links about php. If you are interested, you can take a look
AOP FOR PHP Discussion
[url=http://blog.csdn.net/xiaoxiaohai123/archive/2008/06/30/2598377.aspx]Link tag http://blog.csdn.net/xiaoxiaohai123/archive/2008/06/30/2598377 .aspx[/url]
PHP quasi-AOP implementation
[url=http://hi.baidu.com/thinkinginlamp/blog/item/864a0ef46d93b86eddc474f3.html]Link tag http://hi.baidu.com/thinkinginlamp/blog/item/864a0ef46d93b86eddc474f3.html[/url]
Then I simply implemented it myself
/**
* TSAspect{
* AOP for php
* @package
* @version $id$
* @copyright 2009-2011 SamPeng
* @author SamPeng
* @license PHP Version 5.2 {@link [url=http://www.sampeng.cn]www.sampeng.cn[/url]}
*/
class TSAspect{
/**
* instance
* Reference to the object to which the method belongs
* @var mixed
* @access private
*/
private $instance;
/**
* method
*Method name
* @var mixed
* @access private
*/
private $method;
/**
* aspect
* Aspect save array
* @var array
* @access private
*/
private $aspect = array();
/**
* __construct
* The constructor finds out the implementation methods of all Aspects
* @param mixed $instance
* @param mixed $method
* @param mixed $arg
* @access public
* @return void
*/
public function __construct( $instance,$method,$arg = null ){
$this->aspect = self::findFunction();
$this->instance = $instance;
$this->method = $method;
}
public function callAspect(){
$before_arg = $this->beforeFunction();
$callBack = array( $this->instance,$this->method);
$return = call_user_func_array( $callBack,$arg );
$this->afterFunction();
}
/**
* beforeFunction
* Collection of methods executed before the method
* @static
* @access public
* @return void
*/
protected function beforeFunction(){
$result = $this->getFunction("before");
return $result;
}
/**
* afterFunction
* Collection of methods executed after the method
* @static
* @access public
* @return void
*/
protected function afterFunction(){
$result = $this->getFunction( "after" );
}
/**
* findFunction
* Find all Aspect method collections.
* @static
* @access private
* @return void
*/
private static function findFunction(){
$aspect = array();
foreach ( get_declared_classes() as $class ){
$reflectionClass = new ReflectionClass( $class );
if ( $reflectionClass->implementsInterface( 'InterfaceAspect' ) )
$aspect[] = $reflectionClass;
}
return $aspect;
}
/**
* getFunction
* Call the inserted method
* @param mixed $aspect
* @static
* @access private
* @return void
*/
private function getFunction($aspect){
$result = array();
$array = $this->aspect;
foreach ( $array as $plugin ){
if ( $plugin->hasMethod($aspect ) ){
$reflectionMethod = $plugin->getMethod( $aspect );
if ( $reflectionMethod->isStatic() ){
$items = $reflectionMethod->invoke( null );
}else{
$pluginInstance = $plugin->newInstance();
$items = $reflectionMethod->invoke( $pluginInstance );
}
//处理经过处理的集合
if ( is_array( $items ) ){
$result = array_merge( $result,$items );
}
}
}
return $result;
}
}
interface InterfaceAspect{
public static function getName();
}
class testAspect implements InterfaceAspect{
public static function getName(){
return "这是一个测试AOP";
}
public static function before(){
echo "方法执行之前";
}
public static function after(){
echo "方法执行后
";
}
}
class test{
public function samTest($arg){
echo "这是一个测试方法";
}
}
$test = new test();
$aspect = new TSAspect($test,'samTest');
$aspect->callAspect();
复制代码输出:
方法执行之前
这是一个测试方法
方法执行之后
网友建意:
不懂,,,对开发思想有用不?
网友建意:
思想?这其实也是一种设计模式。。。。将破坏函数单一功能的部分耦合出来。。具体实现有很多办法。。当然PHP的是准AOP。。因为他不能像java那样的编译语言在编译时的时候插入横切面处理过程。
网友建意:
新的事物总是被人华丽的无视。。。
网友建意:
去年就知道面向切面编程了,,,用不上,,,,呵呵,,,
网友建意:
刚刚接触面向函数式编程,Haskell 、Erlang
网友建意:
PHP实现AOP要比Java简单的多..因为有runkit
网友建意:
自己连OOP还没大懂呢。技术啊。追不上。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
