SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Taiwan
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing Object from Extended Class

    Hey all - i haven't dealt much with extended classes up to this point. I'm trying to figure out how i can do something that seems like it should be easy.

    I have a class, lets call it 'humans'. I create a 'humans' object, now if i have extended the 'humans' class with another class called 'male', how can i access the methods and properties of the 'humans' object within the male class?

  2. #2
    SitePoint Evangelist ClickHeRe's Avatar
    Join Date
    Mar 2005
    Location
    Ottawa, Canada
    Posts
    580
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2 things, PHP 4 or 5

    in PHP 5 everything that is declared public or protected can be access by the inherited class be it functions or attributes.

    Code:
    class Human {
    
      private $age;
    
      public function __construct( ) {
    
      }
    
      protected function getAge( ) {
    
        return $age;
     }
    }
    
    class Male extends Human {
    
      public function __construct( ) {
    
        // call parent constructor
        parent::__construct( );
      }
    
      public function print( ) {
    
        echo 'Age: '.$this->getAge( );
      } 
    }
    
    $male = new Male;
    $male->print( );
    
    // error
    $male->getAge( );
    I won't delve into PHP4 territory as I haven't coded OOP in PHP4 very much to make that assessment.
    David

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Taiwan
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Php4

  4. #4
    SitePoint Zealot logitron's Avatar
    Join Date
    Feb 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the PHP 4 equivalent. Basically, public, private, and protected don't exist. Also, for a contructor function, you use the name of the class.

    PHP Code:
    class Human {

      var 
    $age;

      function 
    Human( ) { //Constructor

      
    }

      function 
    getAge( ) {

        return 
    $this->age;
     }
    }

    class 
    Male extends Human {

      public function 
    Male() {

        
    // call parent constructor
        
    parent::Human( );
      }

      function print( ) {

        echo 
    'Age: '.$this->getAge( );
      } 
    }

    $male = new Male;
    $male->print( );

    // Will return age
    $male->getAge( ); 
    Patrick Smith
    PHP Programmer

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    Taiwan
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent - thank you David & Patrick.


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
  •