PHP: self:: vs parent:: with extends
Asked Answered
Z

2

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

Zobe answered 2/1, 2014 at 16:11 Comment(6)
If you've overridden foo() in the Child class, then self::foo() calls the child class version while parent::foo() calls the original parent versionRoer
+1, should have been answerSaga
static::foo() makes it even more fun :)Roer
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:: ?Zobe
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 existsRoer
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 :)Zobe
R
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.

Roer answered 2/1, 2014 at 16:27 Comment(5)
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. ThanksZobe
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!Roer
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 youZobe
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 requirementsRoer
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 ADVANCEChintz
F
0

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

Fraught answered 2/1, 2014 at 16:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.