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还没大懂呢。技术啊。追不上。

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools
