Aspect-Oriented PHP 51
I am Bryan Saunders, and together with John Stamey and Matthew Cameron, we have developed an Extension to PHP to enable you to do Aspect-Oriented Programming with PHP. AOPHP 1.0 currently supports basic Before, After, and Around advice. Future versions will have more advanced support for these three types of advice. The Extension was developed using Java 1.5 and runs on Apache Web Servers. There are two parts to the implementation of AOPHP. The first involves using the Apache module ModRewrite, "the Swiss Army Knife of URL manipulation." The second is the aophpexec.aophp script which calls the AOPHP parser, written in Java 1.5. Aspects are written with a .aophp extension. These aspects are woven into incoming .php files by the Java AOPHP parser, contained in aophp.jar. For more information on AOPHP, visit http://www.aophp.net/ and For information on Aspect-Oriented Programming, visit http://www.aosd.net/ and http://www.aspectorientedprogramming.org/."
Please Explain (Score:3, Interesting)
I've consulted various websites, and I took classes about it, but I still don't get it.
I know it has something to do with modifying the behavior of code after that code has been written. However, all the terminology confuses me. What is it, exactly? What are all the buzzwords? What can and can't it accomplish?
Re:Please Explain (Score:1, Informative)
Re:Please Explain (Score:1)
Re:Please Explain (Score:2)
Re:Please Explain (Score:2, Informative)
Re:Please Explain (Score:1, Informative)
If you use Ruby, for instance, you can do a trick like this to intercept a method call and do your own stuff:
Just imagine some code that
Re:Please Explain (Score:2, Informative)
You define Pointcuts which are points in you application execution such as: before the start of a method that matches a regular expression (like set.*).
You define Advice that is a function/method that is executed at a given pointcut.
And then your code is executed e.g. before a setter method is called on some/any objects. This allows you to open transactions as the
Re:Please Explain (Score:2)
Take for example logging. If you wanted to log the beginning and end of every function, you could edit all the functions and add log calls to the beginning and end. "Aspects" allow you to, for instance, say, you want given code executed for a matching set of functions.
Re:Please Explain (Score:2)
Re:Please Explain (Score:5, Informative)
The reductionist answer is that it lets you attach hooks (i.e. function calls) to various program actions (other function calls, variable accesses, etc.) in a methodical sort of way. The big-picture reason for this is that you can then (in theory) separate out parts of the program that would normally have to be smooshed together into the one module.
For example, say we have a web application framework in which you go from one page to another by calling one of several functions whose names begin with "GotoPage". Now, on this web app, you need to:
In the usual way of doing things, you'd have to put calls to the security and logging subsystems in each GotoPage* function. In AOP, you'd instead write them as separate modules and then hook them to the GotoPage* functions. That way, the different aspects of the problem--page transitions, security and logging--are handled by different modules. This means that:
It seems like a neat idea, although I'm not sold on it yet. One problem that I can see is that the source code doesn't necessarily do what it says it's doing anymore. Buggy aspects can introduce errors into correct code. I'm not sure how/if that's been solved.
(Disclaimer: I haven't actually done any AOP. I've just read about it.)
Re:Please Explain (Score:2)
I mean... properly designed code has a known execution and procedural chart. If you have an object that just makes use of a bunch of other modules, and trusts what they say, you've done your goal of code separation. Aspect
Re:Please Explain (Score:2)
Forgive me if I'm wrong, but isn't this already addressed by object-oriented programming?
Not precisely. The big advantage (AIUI) of AOP is not the idea of putting the different aspects of the program into black boxes--that's already been done. The Big Thing is how the black boxes are connected together, something that is orthogonal to the nature of the black box.
Without AOP, you'd have to explicitly connect the black boxes (be they objects, modules or functions) from inside. AOP lets you connect th
Re:Please Explain (Score:2)
(Pseudocode)
class wrapper
{
oSecurity = NULL;
oLogger = NULL;
wrapper()
{
this->oSecurity = new Security_Object();
this->oLogger = new Logger_Object();
}
logsecurity()
{
this->oSecurity->doSomethingSecure();
this->oLogger->logSomething("Did Security!");
}
}
There. Public interface calls of two black-boxes tied together. All you need to have is the documentation f
Re:Please Explain (Score:2)
It's to save effort in by reducing repetition (Score:3, Informative)
The purpose of AOP is to reduce repetition. Rather than writing repetitious tasks (like eg: security permission checking) into each function over and over, you write it once and attach it to all affected functions.
Taking that example of security, you can do it in non-AOP by just inserting a checkSecurity() call in front of each function - but you might miss some out and it's hard to maintain all that scattered code.
So, the big difference in AOP that I've not
Re:Please Explain (Score:1)
this();
$didthis = 1;
if ($didthis) { that(); }
Re:Please Explain (Score:1)
Honest question: (Score:2)
I'm not trying to dis the community, those are valid concerns and there should be a tool for those people. But ISTM that AOP is much more likely to succeed in Java than PHP; one might a
Re:Honest question: (Score:1)
Re:Honest question: (Score:3, Insightful)
I'm not sure what "thinking in Python" has to do with it though, you can't do a lot of it in Python either!
You can't even dynamically add methods to PHP classes or dynamically modify the methods on an existing class.
Python does that trivially.
There are no hooks called before and after method calls, etc.
There may not be hooks, but it's trivial to do in Python thanks to the previous point. In 2.4, the decorators can do this on a per-method basis, and to translate an entire clas
Re:Honest question: (Score:1)
Re:Honest question: (Score:2)
Re:Honest question: (Score:2)
"Isn't the PHP community mostly just people who want to bash out webpages, with the rest made up of people who think it's a good platform for large-scale frameworks?"
Not quite; you missed out the people who don't think PHP's a good platform for anything significant, but who use
Re:Honest question: (Score:2)
Re:Honest question: (Score:2)
PHP-> servlet translation, now, that would be something (not).
(By the way, I have a fat J2EE book in front of me, and that's what is says at the beginning of the JSP chapter.
Re:Honest question: (Score:2)
Indeed, that what I thought the first time I saw PHP...
Re:Honest question: (Score:1)
AOP is patented (Score:3, Interesting)
Note: yes, I know there is possible prior art to AOP but that doesn't change the fact that the USPTO has issued a patent on it.
Re:AOP is patented (Score:1)
The patent probably wouldn't hold up (Score:2)
IANAL, but I just don't see how they could actually use this patent to trash a specific implementation. They haven't gone after AspectJ so this is probably just a defensi
Re:AOP is patented (Score:1)
Re:AOP is patented (Score:2)
I've said it before (Score:2)
Witchcraft!
Brought to you by a fist shaking, angst ridden C coder.
WTF? (Score:2)
Either I'm nuts or they are.
Re:WTF? (Score:2, Interesting)
Just write a PHP extension.. (Score:3, Informative)
Then all you'd need is a single function to call in PHP like this:
aop_register_advice('my_function', 'my_advice_function', BEFORE_CALL);
aop_register_advice(array($myobj, 'myMethod'), 'my_advice_function', AFTER_CALL);
Code already exists to perform this kind of interception in PHP debuggers / profilers like APD [communityconnect.com] and Xdebug [xdebug.org].
One great aspect of open source is that, frequently, the code you need will probably already have been written. (pun intended)
Re:Just write a PHP extension.. (Score:1)
OK let me get this straight. (Score:2)
OK, i've heard of PHP extensions.
"PHP + compiled Extension"
But now, you want us to do
"PHP + Extension + JVM" ???
I don't want to troll, but... doesn't that make PHP run slower? Of course, if anyone can correct me, please tell me where i'm wrong.
Re:OK let me get this straight. (Score:2, Informative)
You run the PHP through the AOP tool after writing the PHP...new PHP spits out, and then you post *that* code to your site. No slowdown...
Re:OK let me get this straight. (Score:1)
Doesn't do anything new ... (Score:1)