SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP: $this or parent?

    Hi,

    Which is the most correct way to access the methods of a parent class?
    Should one use $this->method() or parent::method()?

    Yours, Erik.

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i use zends double collon for accessing parent classes
    cheers

  3. #3
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    take a look at this:
    PHP Code:
    class father
    {
        var 
    $foo 'me';
        function 
    getFoo(){
            return 
    $this->foo;
        }
    }
    class 
    child extends father
    {
        function 
    getme(){
            return 
    parent::getFoo();
        }
        function 
    getFoo(){
            return 
    'this is my Foo';
        }
    }
    $child = new child();
    echo 
    '->'.$child->getFoo().'<br />';
    echo 
    '::'.$child->getme(); 
    the output is:
    Code:
    ->this is my Foo
    ::me
    So when using :: you always call the parent function, even if the child overwrites the parents function.

    I always use $this->parentfunction(), but not for this reason, as i discovered it just now
    I thought :: is for static calls, so i thought parent::getFoo() shouldn't return anything, but this would only bee if i'd use father::getFoo()

    Do i make sense? nope, at least not to me

    Edit:


    I think parent::function makes only sense when used in the same function in the child class, e.g.
    PHP Code:
    class child{

      function 
    foo(){
         
    $ret parent::foo();
         
    /*
         *do some calculations with $ret, 
         *that are not done in the parent class
         */
        
    return $ret;
     }


  4. #4
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Sutton, Surrey
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use $this->method() if you want the version in the subclass to be executed. If it has not been defined in the subclass it will cascade up to the superclass.

    User parent::method() if you want to execute the version in the superclass and ignore the version in the subclass completely. A common example is parent::__construct() when you want to execute the parent's constructor as well as the child's constructor as PHP does not do this by default.

  5. #5
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks guys.

    Yours, Erik.


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
  •