Back to Top

What is Parent:: and Self:: in PHP

Parent:: and Self:: in PHP

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.

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:

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.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

PHP Error Reporting Levels

Posted on 12 years ago

Bhumi

An Introduction To NoSQL and MongoDB

Posted on 11 years ago

Bhumi

How to use Replace function in MySQL

Posted on 11 years ago

Bhumi