SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Object Aggregation Extension !?!

    Whoa - just saw this for the first time: http://www.php.net/manual/en/ref.objaggregation.php

    Basically this looks like a step in the direction of multiple inheritance. For example;

    PHP Code:
    <?php
    class Foo {
        function 
    hello() {
            echo 
    "Hello World!\n";
        }
    }

    class 
    Bar {
        function 
    goodbye() {
            echo 
    "Goodbye World!\n";
        }

        
    // This is ignored
        
    function hello() {
            echo 
    "Goodbye World!\n";
        }
    }

    $foo = new Foo();

    aggregate($foo,'Bar');

    $foo->hello();

    $foo->goodbye();
    ?>
    Very wierd - haven't seen anyone talking about this. I'm sure Marcus is gonna love it

  2. #2
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is very interesting, it sure looks like the ideal solution for multiple class decorators that we were talking about in this thread

    only problem that I have with it is that it is a PHP extension, and if we have to go that route, I'd almost rather wait to see what PHP 5 comes with "out of the box" to see which is the most compatible alternative.

    Does anyone know if this will be part of the PHP 5 core?

  3. #3
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    This is very interesting, it sure looks like the ideal solution for multiple class decorators that we were talking about in this thread

    only problem that I have with it is that it is a PHP extension, and if we have to go that route, I'd almost rather wait to see what PHP 5 comes with "out of the box" to see which is the most compatible alternative.

    Does anyone know if this will be part of the PHP 5 core?
    Aggregation in PHP5 was dropped, although I could not find the source I origionally found this from.

  4. #4
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by HarryF
    Very wierd - haven't seen anyone talking about this. I'm sure Marcus is gonna love it
    It is almost "Subject Oriented Programming" (I kid you not). You can attach behaviour to a class invisibly, but here without affecting the class hierachy. It is basically the effect of a stateless Mixin class (to use C++ terminology) or a dynamic proxy (in Java) and is exactly what I need for an AOP tool . This is both good and bad.

    It boils down to multiple inheritance being a very dangerous thing. If you can attach stateless services to a class as a Mixin you get things in a much more controlled way. I think Ruby has a similar concept built in (must finish reading the book).

    I think having it as an extension is just about right. You are changing the language in a very fundamental way.

    As for the __call() functionality, is this just a catcher for missing methods like the Perl AUTOLOAD (it looked like it was) or an intercept? If it's a fallback, then it is not so general purpose. It could also render code rather unreadable.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  5. #5
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lastcraft
    As for the __call() functionality, is this just a catcher for missing methods like the Perl AUTOLOAD (it looked like it was) or an intercept? If it's a fallback, then it is not so general purpose. It could also render code rather unreadable.
    Yes, take a look at this... http://www.zend.com/zend/php5/php5-delegation.php

  6. #6
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very interesting article, it looks like _call() will be part of PHP5. For the good, and for the bad

  7. #7
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aggregation in PHP5 was dropped, although I could not find the source I origionally found this from.
    Found a discussion: http://marc.theaimsgroup.com/?t=105659250500001&r=1&w=2

    Oh well...

  8. #8
    SitePoint Enthusiast maetl's Avatar
    Join Date
    Jan 2003
    Location
    Aotearoa
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb



    this sketches a very basic example of cellular membrane computation: it shows how objects in a local region of bounded space can evolve through a series of transformation rules, allowing substitution, aggregation or 'passthrough' operations to change the structure/heirachy of the object sets.

    what reminded me of this was the way that aggregate() 'destroys' the overlapping methods when it merges the classes. this is conceptually very similar to the transformation of sets in the theory of membrane computation.

  9. #9
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for the __call() functionality, is this just a catcher for missing methods like the Perl AUTOLOAD (it looked like it was) or an intercept? If it's a fallback, then it is not so general purpose. It could also render code rather unreadable.
    Think the sole purpose for that for things like web services (and Java integration), so it's easy to "proxy" a remote service while working locally with something that looks like a native object.

    is exactly what I need for an AOP tool
    Can you give a rough idea how AOP would like using this extension? If I have a simple "framework" like this;

    PHP Code:
    <?php
    // A command handling say a form response
    class AddDetails {
        function 
    performAction() {
            echo 
    'Performing AddDetails action<br>';
        }
    }

    // Default action
    class DefaultCommand {
        function 
    performAction() {
            echo 
    'Default action: click <a href="?addDetails=1">add details</a><br>';
        }
    }

    class 
    Controller {
        var 
    $Commands = array();

        function 
    registerAction($field,$class) {
            
    $this->Commands[$field] = $class;
        }

        function 
    isAction() {
            foreach ( 
    array_keys($this->Commands) as $key ) {
                if ( 
    array_key_exists $key$_GET) ) {
                    return 
    $this->Commands[$key];
                }
            }
            return 
    false;
        }
        function 
    run() {

            if ( 
    false !== ($CommandClass $this->isAction()) ) {
                
    $Command = new $CommandClass;
            } else {
                
    $Command = new DefaultCommand();
            }
            
    $Command->performAction();

        }
    }

    $Controller = new Controller();
    $Controller->registerAction('addDetails','AddDetails');
    $Controller->run();
    ?>
    How would I add, say, logging of the actions performed, using AOP and the aggregation extension? I guess it means I'd might aggregate the above AddDetails class with some class LoggerAspect, so I can call a log method on AddDetails. That sounding right?

  10. #10
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One other thing on the AOP front - John seems to be working on something (http://www.coggeshall.org/archives/1...onference.html) which may be useful:

    The purpose of the Philter extension for PHP is to provide a means to access and manipulate a PHP script transparently prior to it being compiled and executed by the Zend Engine.

  11. #11
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Virginia
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by http://www.php.net/manual/en/ref.objaggregation.php
    Aggregation, on the other hand, implies encapsulation (hidding) of the parts of the composition. We can aggregate classes by using a (static) inner class (PHP does not yet support inner classes), in this case the aggregated class definition is not accessible, except through the class that contains it. The aggregation of instances (object aggregation) involves the dynamic creation of subobjects inside an object, in the process, expanding the properties and methods of that object.

    Object aggregation is a natural way of representing a whole-part relationship, (for example, molecules are aggregates of atoms), or can be used to obtain an effect equivalent to multiple inheritance, without having to permanently bind a subclass to two or more parent classes and their interfaces. In fact object aggregation can be more flexible, in which we can select what methods or properties to "inherit" in the aggregated object.


    It boils down to multiple inheritance being a very dangerous thing. If you can attach stateless services to a class as a Mixin you get things in a much more controlled way. I think Ruby has a similar concept built in (must finish reading the book).


    According to the text this in no way implies multiple inheritance. It seems more like a way to build the class structure from the bottom UP and also be able to allocate and deallocate an object instance.

    In other words instead of "element -> atom -> quark" the structure is built "quark -> atom -> element" which actually makes more sense, to me at least.

    Although it seems like an example of how aggregation COULD work - aggregated instances may not override properties or methods; in PHP5 inheritance this is only done if the method or property is private and thus statically bound - it is a leap in programming thought. Take for instance I remember reading on this site recently that someone stated the IDEAL framework would be one which could transform itself on the fly ..



    There was an article one this as well.. I'll find it so later on today..



    Anyhow, using aggregation a framework could conceivably build itself (PULL style) like wact instead of PUSH. By PULL I mean that at first the most basic classes would load and from there depending on what stimulus was needed the framework would aggregate and deaggregate those class structures to accomplish the task.



    Also, in thinking about what maetl stated the possibilities become nearly unlimited and the mind begins to wonder.


    I'm starting to wonder if we should start some sort of petition to get this back into PHP5! It just seems to wonderful to pass up.

    Reading what the core developers wrote about the subject and they seem to of gotten it mixed up with multible inheritance.
    http://www.zend.com/zend/php5/php5-delegation.php


    Anyhow, thank you for the informative post.

    PS:For everyone who was puzzled at first like I was here is a great article on the application of AOP. It's too bad it's not included in PHP5


    http://www.javaworld.com/javaworld/jw-01-2002/jw-0118-aspect.html

  12. #12
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by HarryF
    How would I add, say, logging of the actions performed, using AOP and the aggregation extension? I guess it means I'd might aggregate the above AddDetails class with some class LoggerAspect, so I can call a log method on AddDetails. That sounding right?
    AspectJ has four intercept points to which you can attach code using the "Cool" language. I haven't had a chance to play with it yet. Nanning is simpler. It attempts to attach decorators (actually dynamic proxies) to the classes by intercepting the factory methods that create them. When I last talked to him, he had away of intercepting the constructor too, but I haven't followed it since then. Intercepts are attached with an XML file.

    In the case of logging (a common example) you would have a couple of methods on the logger that was applied to every method on the generated proxy for the target class. One to capture the parameters and one to capture exceptions or the return value.

    The topic has dropped off my radar currently due to more pressing concerns. I was going to revisit when PHP5 was stable. I did have a look at the __call() functionality. It is a fall-through and basically a code obfuscator. The only use for it is "dumb" classes (such as network proxies). I doubt you can safely place these into the rough and tumble of your working class hierarchy without causing confusion .

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  13. #13
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Virginia
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the case of logging (a common example) you would have a couple of methods on the logger that was applied to every method on the generated proxy for the target class. One to capture the parameters and one to capture exceptions or the return value.
    You can do that in PHP too, with a pattern I call Ghost.

    Res

  14. #14
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by Resolution
    You can do that in PHP too, with a pattern I call Ghost.
    Cool bananas - code please .

    The extra step that makes the dynamic proxy patterns into aspects is the management. You attach or replace the code with a central configuration script, usually in a different language. A useful tool would do this without affecting the code you are applying the aspect to. That's the tricky part .

    It is easy to get near misses. I am seriously thinking of tackling this task after SimpleTest. Even in PHP5 I think it will take an extension to do it. Time to brush up on my C++ maybe?

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  15. #15
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Throwing a link: Aop With Php

  16. #16
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Virginia
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lastcraft,
    please refer to this post thread:
    http://www.sitepoint.com/forums/showthread.php?t=150887

    Seems my ghost was just a proxy in disguise


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •