PHP: self:: vs parent:: with extends

24,077
                Child has foo()     Parent has foo()
self::foo()        YES                   YES               Child foo() is executed
parent::foo()      YES                   YES               Parent foo() is executed
self::foo()        YES                   NO                Child foo() is executed
parent::foo()      YES                   NO                ERROR
self::foo()        NO                    YES               Parent foo() is executed
parent::foo()      NO                    YES               Parent foo() is executed
self::foo()        NO                    NO                ERROR
parent::foo()      NO                    NO                ERROR

If you are looking for the correct cases for their use. parent allows access to the inherited class, whereas self is a reference to the class the method running (static or otherwise) belongs to.

A popular use of the self keyword is when using the Singleton pattern in PHP, self doesn't honour child classes, whereas static does New self vs. new static

parent provides the ability to access the inherited class methods, often useful if you need to retain some default functionality.

Share:
24,077

Related videos on Youtube

djkprojects
Author by

djkprojects

Updated on January 03, 2020

Comments

  • djkprojects
    djkprojects over 4 years

    I'm wondering what is the difference between using self:: and parent:: when a static child class is extending static parent class e.g.

    class Parent {
    
        public static function foo() {
           echo 'foo';
        }
    }
    
    class Child extends Parent {
    
        public static function func() {
           self::foo();
        }
    
        public static function func2() {
           parent::foo();
        }
    }
    

    Is there any difference between func() and func2() and if so then what is it ?

    Thank you

    Regards

    • Mark Baker
      Mark Baker over 10 years
      If you've overridden foo() in the Child class, then self::foo() calls the child class version while parent::foo() calls the original parent version
    • Flosculus
      Flosculus over 10 years
      +1, should have been answer
    • Mark Baker
      Mark Baker over 10 years
      static::foo() makes it even more fun :)
    • djkprojects
      djkprojects over 10 years
      OK, but if Child class has no its own definition of foo() then does that mean that there is no difference between the two calls i.e. self:: and parent:: ?
    • Mark Baker
      Mark Baker over 10 years
      If child class has no overridden foo() then it executes the parent foo() code.... there's a difference in the calls, but not in what is executed. Calling parent::foo() will always execute the parent class foo() method, even if the child overrides it; calling self::foo() will execute the foo() override if it exists in self (ie the child), otherwise it will execute the parent foo() if no override exists
    • djkprojects
      djkprojects over 10 years
      thanks, what is the difference then as the result will be exaclty the same ? I understand the difference between self:: and parent:: but not in the above context :)
  • djkprojects
    djkprojects over 10 years
    Thanks, now let's assume that Child hasn't its own foo() and never will then what's the difference between scenario 5 and 6 in terms of using self:: or parent:: keywords? Is there any or they can be used interchangeably? My question refers to these particular scenarios only. Thanks
  • Mark Baker
    Mark Baker over 10 years
    I concede defeat! You've won! If you can make those cast-iron guarantees, and you aren't simply trying to confuse anybody else that looks at your code (including yourself in 6-months time) then there is no difference in what is executed, and you can use them interchangeably if you really, really want to!
  • djkprojects
    djkprojects over 10 years
    Thanks Mark, to not confuse anybody is there any preferable version for these scenarios? I suppose that parent::foo() is more explanatory. What would be your approach ? Thank you
  • Mark Baker
    Mark Baker over 10 years
    The method I would probably take is to make use of late static binding, and to use static::foo() unless I specifically wanted to force calling parent::foo().... that allows me to implement a foo() in the child should I wish to do so in the future... and even to extend the child class still further... though a lot of it comes down to specific requirements