Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Class in class.

  1. #1
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Class in class.

    I'm experimenting a little with classes again and I would like to know how to call a method from a sibling (another child the parent class) class (if it is posible).
    Why I was experimenting I found out that:
    PHP Code:
    <?
    class foo
    {
        public 
    $value true;

        function 
    greet()
        {
            print 
    'Hello!';
        }
    }
    class 
    bar extends foo
    {
        public 
    $value true;

        function 
    __construct()
        {
            
    parent::greet();
        }
    }
    ?>
    classes (foo and bar) are parent and child but these:
    PHP Code:
    <?
    class foo
    {
        public 
    $bar '';

        function 
    __construct()
        {
            
    $this->bar = new bar();
        }

    }
    class 
    bar
    {
        public 
    $value true;
        function 
    __construct()
        {
            
    parent::greet();
        }
    }
    ?>
    are not (foo is not recognised as bar's parent).
    I can access any method from any place above bar (eg. $foo->bar->method or $this->bar->method) but I can't do access anything from bar. Is it possible? How? I'm sure I'm msissng something I shouldn't.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,047
    Thanks
    10
    Thanked 93 Times in 91 Posts
    In your second class there is no parent->child relationship , that only happens when you enforce such with extends() , since you are using PHP5 you can also use interfaces (implements) to show a different type of relationship.

    Your second example is an example of composition/aggregation there are aggregate functions in PHP4 but I have never used them.

    Object composition is a common method of working with groups of classes without inheritance and with more flexibility than inheritance , you can only have 1 parent class in PHP , ok that parent can itself extend another class but it gets difficult to manage complex code in a single heirachy and therefore limits complexity.

    extended classes generally extend another clase with related/similar functionality , composite/aggreate classes and methods need not be related at all to the calling object.

    e.g.
    class human{}
    class male extends human{}
    class female extends human{}

    so far thats workable, but what about

    class girlfriend{}

    OK girlfriend is indeed human , but in relation to our instance of human its not relevant. Multiple inheritance would pull that together but 1) PHP don't have multiple inheritance & 2) It never will have

    so girlfriend is a good class to hold in an instance of our man or woman
    class man extends human{
    function girlfriend( $array_stats ){
    $this->girlfirend = new girlfriend( $array_stats ) ;
    }
    }

    now how you access the methods and properties of girlfriend is up to you though there is no 1 correct way to to it (in code or real life )

    PHP Code:
    <?
    class foo{
        function 
    __construct(){
            
    $this->bar = new bar$this );
            
    $this->bar->reply();
        }

        function 
    greet(){
          echo 
    'hello';
       }

    }
    class 
    bar{
        function 
    __construct$calling_object ){
            
    $calling_object->greet();
             
    /*or indeed*/
             
    foo::greet();
        }
         function 
    reply(){
           echo 
    'hello back';
         }
    }
    ?>
    Last edited by firepages; 09-25-2004 at 04:03 AM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Thank you! I did not want multiple inheritance, I can live without it. Though the $calling_object restores a 'broken link' from the from the inner class to the outer class. I only wanted to access the methods from the other subclass from the first one. Some code to clarify what I was talking about):
    PHP Code:
    <?
    class main

        function 
    __construct()
        { 
    // This creates the links to sub-classes of this class
            
    $this->target = new target($this);
            
    $this->bar = new bar($this);
            
    /*
                $this needs to be sent becouse the subclass would not
                be able to use the methods and propertys of the main class.
                PHP doesn't do anything to allow this, so you must do it yourself.
            */
        


    }

    class 
    bar
    // The first sub-class.
        
    public $parent;
        public 
    $other_class;
        function 
    __construct($object)
        { 
    // This makes a connection to the main class.
            
    $this->parent $object;
             
        } 
         function 
    reply(){ 
           echo 
    'hello back' "\n";
           
    // Now we can call a method of the main class or any other sub-class the main class has.
           
    $this->parent->target->reply();
           
    // or we can link the classes together if we need to use it more than once.
           
    $this->other_class $this->parent->target;
           
    $this->other_class->reply();
         } 
    }

    class 
    target
    // The second sub-class. 
        
    function reply()
        { 
            echo 
    'hello from target' "\n"
        }
    }

    $test = new main();
    $test->bar->reply();
    /*
        Use: If you want PHP code to be structured in a hierarchical way
        (you call all methods in any subclass like $main_class->sub_class->method)
        way and want to be bale to get data from another branch of classes.
    */
    ?>
    The only question to end my test is: Is this a good Idea to hierachicly set classes? Is it efficient?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,047
    Thanks
    10
    Thanked 93 Times in 91 Posts

    Post

    The only question to end my test is: Is this a good Idea to hierachicly set classes?
    Well the point really is that you are not setting a heirarchy as such with composition/aggregation of other classes.

    A true parent-child relationship requires that the parent class extends a child class and that is not the case in your code above.

    My only complaint about the code above would be the usage of `$parent` & herarchical terms for your 'sub-classes' as they are not subclasses & there is no heirarchy , they are composite/external objects , semantics I know , but one day they come back to bite you

    The danger of working like this is that you can end up in class $x calling a method of class $b that actually gets passed via class $g & $r along the way , it should be calling class $b directly , not only from an efficiency standpoint but also in readability.

    e.g.

    $this->parent->target->reply();

    is about as far away as you want to get in terms of abstraction , any further than that and you head will hurt !
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Thank you for the reply. One more thing: What is the realtion of the classes in the main class to the main class? You said they are 'stand alone' but you can't say they have no relation at all. How is that relation called?
    I now have some sort of proof that there is some kind of hierarchy relationship (print_r($GLOBALS['test']) from that code).

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,047
    Thanks
    10
    Thanked 93 Times in 91 Posts
    The objects instansiated within your classes are composite objects , I don't know any other way to describe them.

    More information can be gleaned at http://www.php.net/manual/en/ref.objaggregation.php

    but with your current composite objects there is no child-parent relationship even though the composite objects 'belong' to the main class.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Well from the manual it looks like that it is only possible in PHP but they don't really know how they want to be in the end yet.
    So there is no parent-child relationship but they are connected in another way "aggregated" right?


  •  

    Posting Permissions

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