php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53705 Add AOP & Interception Filter Support
Submitted: 2011-01-10 07:59 UTC Modified: 2011-01-10 08:20 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: inbox at astions dot com Assigned:
Status: Wont fix Package: *Programming Data Structures
PHP Version: Irrelevant OS: NA
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: inbox at astions dot com
New email:
PHP Version: OS:

 

 [2011-01-10 07:59 UTC] inbox at astions dot com
Description:
------------
I propose a modification to the PHP core adding the following features. (Not all inclusive, I'll try to include everything I think needs to be added).

I guess this could be called interception. The official name is AOP.  What it would enable you to do is intercept method calls immediately before and after they are executed. It would also enable you to filter the arguments sent to the methods.

We would need:
A way to enable this feature.
A way to add interceptions
A way to list interceptions
A way to remove interceptions
The ability to view interceptions in standard debug backtraces.

Test script:
---------------
interception::attachBefore('className', 'methodName', 'classToExecute', 'methodToExecute');
interception::attachFilter('className', 'methodName', 'classToExecute', 'methodToExecute');
interception::attachAfter('className', 'methodName', 'classToExecute', 'methodToExecute');

interception::detachBefore('className', 'methodName', 'classToExecute', 'methodToExecute');
interception::detachFilter('className', 'methodName', 'classToExecute', 'methodToExecute');
interception::detachAfter('className', 'methodName', 'classToExecute', 'methodToExecute');

$list = interception::getAll();
$list = interception::getForClass('className', [methodName]);

interception::clearAll();

Expected result:
----------------
When attaching an intercept before a method call, the methodToExecute will receive the exact same arguments as the intercepted methodName. Immediately after methodToExecute has completed, methodName will then be executed.

When attaching an intercept filter, the methodToExecute method will receive the exact same arguments as the intercepted methodName passed by reference (maybe). It can then modify the arguments if required.

When attaching an intercept after a method call, the methodToExecute will receive the exact same arguments as the intercepted methodName in addition to the return value of methodName.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-10 08:05 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 [2011-01-10 08:05 UTC] rasmus@php.net
You could do exactly the same thing with a __call() wrapper method in the class 
assuming you are able to modify the class and make the methods provate so they can 
only be called via the __call() wrapper.

The overhead of checking every function call for before, filter and after trigger 
methods in an interpreted scripting language prevents this from being a realistic 
feature request.
 [2011-01-10 08:08 UTC] inbox at astions dot com
I believe the overhead would be negligible or non-existent because:

1.  The feature could be implemented in a way where it has to be explicitly enabled.
2.  The lookups could use array keys which would be very fast and only add a few CPU cyles to each method call.
 [2011-01-10 08:20 UTC] rasmus@php.net
But it is an extra lookup on every function call to check if it it enabled which 
means all existing applications will be affected.  And once enabled, every call on 
the class it was enabled on would be stuck with another 3 lookups.  This is too 
much damage for a feature almost noone would use.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC