SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    earth
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Classes talking to each other?

    Hi, I am trying to make a class modify another class's variable.
    PHP Code:
    <?

    $spoon 
    = new Spoon;
    class 
    Spoon {
        var 
    $test 'hello';
        
        function 
    Spoon() {
            
    $fork = new Fork($this);
            print 
    $test;
        }
    }

    class 
    Fork {
        var 
    $spoon '';
        
        function 
    Fork($spoon) {
            
    $this->spoon = &$spoon;
            
    $this->spoon->test 'hello modified';
        }
    }
    ?>
    In this example, the Fork class is not able to change the $test variable in Spoon. how can I make it that any changes to $this->spoon in Fork class is reflected on the spoon class? any help is appreciated. thanks!

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function Fork(& $spoon) { //notice the &
            
    $this->spoon = &$spoon
            
    $this->spoon->test 'hello modified'
        } 
    then it should work, as at the moment your only making a copy of $spoon

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    earth
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Daimaju: that did the trick. THank you so much


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
  •