SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    best practice ? accessing composite methods

    Hi , when using composite objects within a class , what is considered 'best practice' when accessing methods of the composite classes ?

    Note I am extending some php-gtk built-in classes which have their own methods, I can call those directly ($obj->buit_in_method()) , or abstract them as below with avoid_overload()

    PHP Code:
        <?
        
    class gfi_progress extends GtkProgressBar{
           function 
    gfi_progress$adjustment ){    
                
    $this->GtkProgressBar$adjustment ) ;
            }
            
    /*
               set_value is a built in method of GtkProgressBar
            */
            
    function avoid_overload$int ){
               
    $this->set_value$int );
            }
            function 
    custom_routine($blah){
               
    /*my own custom method*/
            
    }
        }
        
    ?>
    so far so good , now when I use this object as a composite object ...
    PHP Code:
        <?
        
    class gfi_progress_dialog extends GtkWindow{
            function 
    gfi_progress_dialog$args ){
                
    $this->GtkWindow();
                
    $this->progress = new gfi_progress( new GtkAdjustment );
                 
    /*add is a GtkWindow built in method*/
                
    $this->add($this->progress);
                 
    //blah//
            
    }
        } 
        
    ?>
    now if I am wondering the best way to call custom_routine() in the composited class ,

    e.g. I can simply call ...
    $gfi_progress_dialog->progress->custom_routine();

    <edit>actually with PHP<5 its not that easy you need to dereference the object first ~ but you lknow what I mean</edit>

    or ... should I create a custom_routine method in $gfi_progress_dialog that calls the composite method ?

    PHP Code:
        <?
        
    function custom_method(){
           return 
    $this->progress->custom_routine();
        }
        
    ?>
    I am sure there are pros and cons to both but I cant weigh it one way or another ... what might be considered best practice and why ?


    also , I just been staring at the aggregate functions in the php manaul .. any help/point ?

  2. #2
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did I ask a stupid question ? (quite possible I accept )

    This is bugging me more daily , mostly I am doing what I dont really want to & creating accessors in the top level to access composited methods , but when they mirror almost verbatim the composite classes method I guess I am doing something wrong.

    In the same way , dereferencing the composite object just to get at a simple method is also annoying , I know PHP5 will make this easier but I need to be BC at this point.

    aggregation almost seemed interesting on paper but it seems that the Gtk classes don't want to play & I know I am in trouble when multiple inheritance actually begins to look promising

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a look also at the Composite pattern a while back, and ran into the same problem you now have it seams, from what I can tell?

    On the recommendation of Lastcraft, I looked at the Visitor pattern and arrived at some script which did work as intended from the point of view of the 2 patterns, the thread is below.

    Maybe if you have a look at the script I posted you can gleam something from it? The visitors in question in my script are basic Printers, though this is not to say that you can't do something else with the Visitor, in regards to what you want to do

    If this is what your looking for, and you need some help, I'll try to help though I've never used GTK before so beware...

    http://www.sitepoint.com/forums/showthread.php?t=221179

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The question is valid, and probably ends up with similar questions to are getters/setters evil? In my opinion, this is a slippery slope towards exposing too much of the internal structure of your object. You might end up with some unwieldy, fragile statement like:
    PHP Code:
    $application->window->control_set[2]->control[1]->check(); 
    where as you might be better off with some kind of an API like
    PHP Code:
    $control =& $application->findControlById(12);
    $control->check(); 
    The second might be more lines of code, and introduces an intermediate object, but by using a finder method on top of your aggregates, it my allow you to alter the structure some without changing the client code.

    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I've just missed something...

    Nevermind, maybe the next time

  6. #6
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies , yeah it is getting messy , I looked at the visitor stuff you linked to Dr L but I have to say it lost me
    Though it does look like an intermediate object is going to come into this somewhere as sweatje says.

    Gonna scratch my head a bit more and see If I can re-organise the code to try and avoid these issues in the first place , these classes are quite small currently and perhaps I am trying to split them up too much.

    I dont mind dereferencing 1 level , that I can handle , but yes I really want to avoid the $application->window->control_set[2]->control[1]->check(); scenario which is where I am currently heading !

    cheers for the feedback

  7. #7
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    firepages, if you are using PHP5, why would you need to dereference anything? Maybe I don't understand what you mean, since I hear 'dereference' and I picture that you are having to clone() your objects. Is there another way to dereference in PHP5?
    Garcia

  8. #8
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not using PHP5 , currently PHP-GTK is PHP4 only though PHP-GTK2 ( GTK2 + Zend2 ) is now under development.

    what I mean by dereference here is that currently
    $this->composite_object->composite_method();

    dies (in php-gtk) & I have to do this ...
    $composite_object = $this->composite_object ;
    $composite_object->composite_method() ;

    and it gets worse for more complicated relationships, which, I am trying to avoid in the first place ... hence this post.


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
  •