Object-oriented programming becomes an essential for the developer. The use of self and parent keyword is important if you going to use OOP programming in PHP.Here is one quick article about two PHP keywords.When you need to access a static property inside a class, you can use the magic keywords self and parent which determines the current class and the parent of the current class respectively.Also, To access internal const variables or static methods, You must have to use self:: with the operator, you can not use $this.
If you want to access PHP Class member Variables or method which are declared as const (using constant keyword) or static method, you must have to use the scope resolution (::) operator because you can not call using -> operator.
What is Self Keyword?
self is a pointer to the class. Usually self to refer to the class using static variables. If you use a static method inside the class, you can use the self-keyword to call. Self-keyword to call a static variable must use ::
self keyword used for the current class and basically it is used to access static members, methods, and constants whereas parent:: keyword used for the parent class and it is mostly used when you want to call the parent constructor or methods. It also is used to access members and constants.
What is Parent Keyword?
$this only represents the current instance of the class and self:: represents the class itself.You can not use this operator outside the class code and also in extended class to get parent class values.
You can use parent:: keyword as opposite of the parent’s class name because it is easy to change your class hierarchy because you are not hard-code the parent’s class name.parent:: is the special name for parent class which when used in a member function.To use the parent to call the parent class constructor to initialize the parent class so that the object inherits the class assignment to give a name.
NOTE: PHP does not accept parent as the name of a function.
Let’s have a look into both parent:: and self:: for accessing the Child and Parent classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Parentcls { const CLSPARENT = "Parent"; function __construct() { echo "Within " . self::CLSPARENT . " constructor."; } } class Childcls extends Parentcls { const CLSCHILD = "Child"; function __construct() { parent::__construct(); echo "Calling " . parent::CLSPARENT . " class"; echo "Within " . self::CLSCHILD . " constructor"; } } $obj = new Childcls(); |
As per above example, static methods are part of the class but they are not bound to any specific object instance and its properties. Therefore, You can not access static methods using $this but you can access class itself by using self.To access a method in the parent class that’s been overridden in the child by using the parent:: keyword.In the example, we override the constructor function in the child class using prefix parent but you can override any function.
Output:
1 2 3 4 5 | Within Parent constructor. Calling Parent class Within Child constructor |
Difference between $this,self and Parent
$this
1. $this refers to the object.
2. $this point to the current object instance does not point to any other object or class.
::self
1. self:: refer to the class itself, not point to any object.
2. using self:: you can access the static properties and static methods of the current class.
::parent
1. parent:: is refer to the parent class.
2. using parent::, you can access the static properties and static methods of the parent class.
With the use of self and parent, you allow to avoid explicitly reference the class by name.
Do let me know how you are using parent and self in your application via the comment section. Don’t forget to share this article on Google plus and Twitter.