SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $this vs self:: vs parent:: vs $object_name

    Can anyone tell me what's the best practice for using these -- as in when it's best to use each one?

  2. #2
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You seem confused.

    • $this is used when inside a class method and referring to the object the method belongs to:
      PHP Code:
      class Bar {
          protected 
      $property 'foo';
          public function 
      foo() {
              echo 
      $this->property;
          }

    • self:: is used when inside a method and referring to the class the method belongs to:
      PHP Code:
      class Singleton {
          protected function 
      __construct() {}
          public static function 
      getInstance()
              static 
      $instance;
              if (!isset(
      $instance)) $instance = new self;
              return 
      $instance;
          }

    • parent:: is used when inside a method (static or not) and you wish to invoke a method in the parent class:
      PHP Code:
      class Foobar extends Bar {
         protected 
      $bar 'bar';
         public function 
      foobar() {
            
      parent::foo();
            echo 
      $this->bar;
         }

    • $object is used when you have assigned an instance of a class to the said variable:
      PHP Code:
      $bar = new Bar;
      $bar->foo(); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2006
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets say I'm extending a class. Couldn't I use both:
    $this->classMethod()
    or
    parent::classMethod()


    When I'm not extending a class, Couldn't I use both:
    $this->classMethod()
    or
    self::classMethod()

  4. #4
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tdnxxx444
    Lets say I'm extending a class. Couldn't I use both:
    $this->classMethod()
    or
    parent::classMethod()
    Those two wouldn't be the same if you had defined classMethod in both the parent and the child.
    When I'm not extending a class, Couldn't I use both:
    $this->classMethod()
    or
    self::classMethod()
    The latter would be appropriate only if the method was static. This does actually create a somewhat odd discrepancy between parent:: and self::, and the manual doesn't have anything on it. You could think that self:: was for static calls only, whereas parent:: is for both types with the child class providing the context ($this) if there is one.


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
  •