0

What is parent:: and self:: in php

Spread the love

Use of parent:: and self:: in php

Object-oriented programming becomes an necessity 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.

Object-oriented programming becomes an necessity for the developer.Self and Parent keywords are must if you are programming in PHP.Today in this article i am going to tell you about these two keywords and belive me it is not so hard.

Self ::

When you have to get to a static property inside a class, you can utilize the enchantment words self. To get to inner const factors or static techniques, You should need to utilize self:: with the administrator, you can not utilize $this.

Parent ::

parent determines the current class and the parent of the current class respectively.

In the case that you need to get to PHP Class part Variables or technique which are pronounced as const (using constant keyword) or static method, you should need to utilize the scope resolution (::) operator since you can not call utilizing – > operator.

What is Self :: ?

self is a pointer to the class. Typically self to allude to the class utilizing static factors. In the event that you utilize a static strategy inside the class, you can utilize the self-catchphrase to call. Self-watchword to call a static variable must utilize ::

self keyword utilized for the present class and fundamentally it is utilized to get to static members, methods, and constants though parent:: utilized for the parent class and it is for the most part utilized when you need to call the parent constructor or methods. It additionally is utilized to get to individuals and constants.

What is Parent :: ?

$this just speaks to the present occasion of the class and self:: speaks to the class itself.You can not utilize this operator outside the class code and furthermore in extended class to get parent class values.

You can utilize parent:: keyword as inverse of the parent’s class name since it is anything but difficult to change your class progressive system since you are not hard-code the parent’s class name.parent:: is the exceptional name for parent class which when utilized as a part of a part function.To utilize the parent to call the parent class constructor to initialize the parent class so that the object acquires the class task to give a name.

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.

According to above case, static techniques are a piece of the class yet they are not bound to a particular object instance and its properties. In this manner, You can not get to static methods utilizing $this but rather you can get to class itself by utilizing self.To get to a strategy in the parent class that has been overridden in the child by utilizing the parent:: keyword.In the example, we override the constructor function in the child class utilizing prefix parent however 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.

Leave a Reply

Your email address will not be published. Required fields are marked *