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.
Related videos on Youtube
Author by
djkprojects
Updated on January 03, 2020Comments
-
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 over 10 yearsIf you've
overridden
foo() in the Child class, thenself::foo()
calls the child class version whileparent::foo()
calls the original parent version -
Flosculus over 10 years+1, should have been answer
-
Mark Baker over 10 years
static::foo()
makes it even more fun :) -
djkprojects over 10 yearsOK, 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 over 10 yearsIf child class has no overridden
foo()
then it executes the parentfoo()
code.... there's a difference in the calls, but not in what is executed. Callingparent::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 over 10 yearsthanks, 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 over 10 yearsThanks, 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 over 10 yearsI 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 over 10 yearsThanks 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 over 10 yearsThe method I would probably take is to make use of late static binding, and to use
static::foo()
unless I specifically wanted to force callingparent::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