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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool referencing a class inside another class

    hi, i've been trying to use the functionality of one class inside another:

    should the following code work in theory?

    PHP Code:
    class out {
        var 
    $output "yay";
        function 
    put() {
            echo 
    $this->output;
        }
    }

    class 
    test {
        function 
    testme() {
            global 
    $out;
            
    $out->put();
        }
    }

    $out = new out;
    $test = new test;

    $test->testme(); 
    the above should output yay, right?

    thanks
    j
    Last edited by johnrau; 06-07-2004 at 02:22 AM.

  • #2
    New Coder
    Join Date
    Jun 2004
    Location
    Newcastle, England
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i dont THINK its right in theory, in fact fairly sure it isnt. But then I don't know how to do it either so i cud b wrong

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    well it works , though in general OO advocates try to avoid globals where possible

    2 more commonly use options are ,
    1) to pass the object to the constructor (or indeed a class method)
    PHP Code:
    <?
     
    class out 
        var 
    $output "yay"
        function 
    put() { 
            echo 
    $this->output
        } 


    class 
    test 
        function 
    test$obj ){
          
    $this->obj=$obj;
        }
        function 
    testme() { 
            
    $this->obj->put(); 
        } 


    $out = new out
    $test = new test($out); 

    $test->testme();
    ?>
    or 2) 'aggregate` the class.

    PHP Code:
    <?
     
    class out 
        var 
    $output "ya2y"
        function 
    put() { 
            echo 
    $this->output
        } 


    class 
    test 
        function 
    test( ){
          
    $this->out=new out;
        }
        function 
    testme() { 
            
    $this->out->put(); 
        } 


    $test = new test( ); 
    $test->testme();
    ?>
    ehat you use and when you use it will depend on many factors.
    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)

  • #4
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you sir!
    Your code has been much helpful!

    Have a good one!

    -John


  •  

    Posting Permissions

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