instanceof php – PHP instanceof Keyword | PHP Late Static Binding

in instanceof php, PHP instanceof | PHP Late Static Binding – Generally when we create a class, we need to mix Current Class and Parent Class. To refer to the current class, we use self:: Expression, while to refer to the parent class, we use parent:: Expression.

PHP provides us self and parent keywords so that our codes are not bound by any name in any way, so that we can change the name of any class according to our need.

instanceof php – PHP instanceof | PHP Late Static Binding

If we refer to classes in our code by their actual name instead of self or parent, then our code is completely bound to that class.

As a result, if we change the class name, then we have to check all the codes specified in that class and wherever we have specified the class name, at all those places we have to manually change the class name. have to do.

But when we use self or parent keywords, then the matter is no longer like this. By specifying Current Class as self and Parent Class with parent keyword, we can change the name of our class whenever we want and we do not even need to look at the codes of those classes.

Self and parent keywords do their work in a normal way, so we do not need to think much about them. The self keyword refers to the own class in the method, while the parent keyword refers to the parent class of the current class.

But when we talk about Static Members, we need to understand it in some detail. Since Static Members Defined in a Class are Class Level, So when we Derived a Class which already has a Static Method, then Calling this Static Method for Derived Class will result in Static Method of Base Class. , refers to the base class only.

Whereas sometimes the need is such that we have to execute some code while deciding which Class Context i.e. Class Scope we are in. If we are in Base Class’s Scope, then Base Class’s Context should contain Static Method Execute, whereas if we are Derived Class’s Context, Derived Class’s Context should contain Static Method Execute. To understand this let us look at an example.

The following example is clarifying how Static Binding is generally performed and PHP always refers to the Context of the Base Class. Then in the next program we will convert this Static Binding to Late Static Binding.

Static Binding is always performed when we refer a static method to a method of a class by self keyword, because self always refers to the class in which the method is defined and that class is never referred to. does not, in which it is available because of the Derivative due to Inheritance.

Don’t Miss : Object-Oriented Programming Concepts in PHP

When we use self:: Expression or __CLASS__ Constant in a class, then this Expression or Constant, Invoked Method represents the class name of the class in which it is. eg:
instanceof php


In this program, we have created a static method named who() in Product Class and while inheriting Product Class in MainProduct Class, we have also created a method named who() in MainProduct Class, which is who of Product Class. Overrides the () method. Also we have created a method named getInfo() in Product class, which invokes a static method named who() of its own class.

When this program is run, the following statement is executed and the getInfo() method is called for the MainProduct class:

MainProduct::getInfo();

Since we have Derived Product Class in MainProduct Class and getInfo() method of Base Class is in public scope, Derived Class is also available in MainProduct. As a result, when the above statement is executed, the getInfo() method of the Product class should be invoked for the Derived Class i.e. MainProduct Class.

Because the getInfo() method is called for the MainProduct class, the who() method of the MainProduct class itself should be invoked by the getInfo() method. But this does not happen and we get the output of this program as follows, and the above statement is calling the who() method of the Base Class itself, which is returning the name of the Base Class itself:

Output

Product

This behavior of Static Method is called Static Binding.

Since our need is not being fulfilled by Static Binding because we want that the class name with which we have called Static Method should be Return, so to fulfill this requirement PHP provides us with static keyword .

We can get the facility of Late Static Binding from PHP by using this keyword instead of the self keyword specified in the previous program. That is, if we use the static keyword as follows in place of self in the above program itself, then the result we get will change:
instanceof php



Output

MainProduct

As we can see in the output of the above program that now the MainProduct::getInfo() statement is not returning the name of the base class but the name of the class with which we have called the getInfo() method.

This happens because static:: Expression is used under Late Static Binding and the method in which it is used does not access the static member with reference to the class in which the method is in, but rather At run time, it decides for which class the static method is used.

If Static Method is used for Base Class, then this Static Keyword Method refers to the Scope of Base Class whereas if Static Method is used for Derived Class, this Keyword refers to the Scope of Derived Class.

This Keyword always accesses Private Data and Methods of Object Level i.e. Same Scope, whereas using static:: generates different type of result, as we can understand by previous programs. Whereas another feature of the static keyword is that static:: can always refer only to the static properties of a class.

If we want to access any property of Object i.e. Data Member of Object Level, then we have to use this only, whereas to access static Members we have to use static:: only, because Static Members are always Class Level. There are.

In simple words, parent:: or self:: is always forward in the calling information. That is, when using them, in the Hierarchy of PHP Control Inheritance, it is forwarded from Child Class to Base Class, while using static:: it is not Forwarding and Static Method is specified in the class, static:: Same. limited to the class. As a result it always refers to the class for which the static method is invoked.

PHP instanceof Keyword

Using this keyword, we can find out whether an object is an instance of a Class, Sub-Class or Implemented Interface or not. For example, if we want to know in the above program whether Object $cpu is an instance of Box class or not, then we can create an if statement as follows to know this:

//instanceof php
if($cpu instanceof Box)
{
	echo “Object ‘cpu’ is an Instance of Class Box”;
}

There are basically two things to keep in mind while using this keyword. The first is that the name of the class does not have to be written between the quotes. If we write the class name between the quotes, then PHP triggers an error and secondly, if the comparison fails, the script’s execution is aborted.

The instanceof keyword proves to be very useful in the case when we are simultaneously processing with many objects in our program.

Leave a Comment