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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Problem echoing a variable in OOP

    PHP Code:
    <?php 

    class Tree{

        private 
    $_Type;
        private 
    $_Height 1;
        
        public function 
    setType($_TheType){
            
    $_Type $_TheType;
            echo 
    '<b>'.$_Type.' tree has been cultivated.</b><br/>';
            echo 
    'The height of '.$_Type.' Tree is '.$this->_Height.' meters<br/>';
        }
        
        public function 
    oneYearPasses(){
            
    $this->_Height++;
            echo 
    'The'.$this->_Type.' grows to '.$this->_Height.' meter <br/>';
        }
        
    }

    $OrangeTree = new Tree;
    $OrangeTree->setType('Orange');
    $OrangeTree->oneYearPasses();

    $AppleTree = new Tree;
    $AppleTree->setType('Apple');
    $AppleTree->oneYearPasses();

    $MangoTree = new Tree;
    $MangoTree->setType('Mango');
    $MangoTree->oneYearPasses();
    ?>
    Why doesn't oneYearPasses method echo's the $_TYPE in the echo? echo 'The'.$_Type.' grows to '.$this->_Height.' meter <br/>';
    Last edited by MHaris; 08-22-2007 at 03:40 AM.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    PHP Code:
     $_Type $_TheType
    should be
    PHP Code:
     $this->_Type $_TheType
    A better way to do this:

    PHP Code:
    <?php 

    class Tree{

        private 
    $_Type;
        private 
    $_Height 1;
        
        public function 
    setType($_TheType){
            
    $_Type $_TheType;
            
    $return_val '<b>'.$_Type.' tree has been cultivated.</b><br/>';
            
    $return_val .= 'The height of '.$_Type.' Tree is '.$this->_Height.' meters<br/>';

            return 
    $return_val;
        }
        
        public function 
    oneYearPasses(){
            
    $this->_Height++;
            
    $return_val 'The'.$this->_Type.' grows to '.$this->_Height.' meter <br/>';

            return 
    $return_val;
        }
        
    }

    $OrangeTree = new Tree;
    echo 
    $OrangeTree->setType('Orange');
    echo 
    $OrangeTree->oneYearPasses();
    Last edited by PappaJohn; 08-22-2007 at 03:52 AM. Reason: added info

  • Users who have thanked PappaJohn for this post:

    MHaris (08-22-2007)

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    returning rather than echoing is often a better idea, but losing the constructor in place of a setter doesn't really make sense in this case, it allows you to change the type of the tree as many times as you like!
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

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