29

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

djkprojects
  • 425
  • 1
  • 5
  • 8
  • 18
    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 – Mark Baker Jan 02 '14 at 16:13
  • 1
    +1, should have been answer – Flosculus Jan 02 '14 at 16:13
  • 1
    `static::foo()` makes it even more fun :) – Mark Baker Jan 02 '14 at 16:15
  • 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:: ? – djkprojects Jan 02 '14 at 16:18
  • 2
    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 – Mark Baker Jan 02 '14 at 16:20
  • 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 Jan 02 '14 at 16:24

2 Answers2

64
                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.

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 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 – djkprojects Jan 02 '14 at 16:38
  • 2
    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! – Mark Baker Jan 02 '14 at 16:41
  • 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 – djkprojects Jan 02 '14 at 16:49
  • 6
    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 – Mark Baker Jan 02 '14 at 17:03
  • Hi @Mark Baker + 1 - when using static:: with inheritance, php will first look in the initial calling class and if it doesn't find it goes and looks for the parent class? THANKS IN ADVANCE –  Feb 03 '23 at 13:51
0

self is used to call static function and manipulate static variables, which are class specific not object specific.

Naresh Walia
  • 2,012
  • 2
  • 16
  • 16