Php instanceof static class

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.

Читайте также:  Python egg info directory

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.

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

 public static function getInfo() < self::who(); >> class MainProduct extends Product < public static function who() < echo __CLASS__; >> MainProduct::getInfo(); ?>

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:

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:

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

 public static function getInfo() < static::who(); >> class MainProduct extends Product < public static function who() < echo __CLASS__; >> MainProduct::getInfo(); ?>

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)

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.

Источник

Type Operators

instanceof is used to determine whether a PHP variable is an instantiated object of a certain class:

Example #1 Using instanceof with classes

 class MyClass  < >class NotMyClass  < >$a = new MyClass; var_dump($a instanceof MyClass); var_dump($a instanceof NotMyClass); ?>

The above example will output:

instanceof can also be used to determine whether a variable is an instantiated object of a class that inherits from a parent class:

Example #2 Using instanceof with inherited classes

 class ParentClass  < >class MyClass extends ParentClass  < >$a = new MyClass; var_dump($a instanceof MyClass); var_dump($a instanceof ParentClass); ?>

The above example will output:

To check if an object is not an instanceof a class, the logical not operator can be used.

Example #3 Using instanceof to check if object is not an instanceof a class

 class MyClass  < >$a = new MyClass; var_dump(!($a instanceof stdClass)); ?>

The above example will output:

Lastly, instanceof can also be used to determine whether a variable is an instantiated object of a class that implements an interface:

Example #4 Using instanceof with interfaces

 interface MyInterface  < >class MyClass implements MyInterface  < >$a = new MyClass; var_dump($a instanceof MyClass); var_dump($a instanceof MyInterface); ?>

The above example will output:

Although instanceof is usually used with a literal classname, it can also be used with another object or a string variable:

Example #5 Using instanceof with other variables

 interface MyInterface  < >class MyClass implements MyInterface  < >$a = new MyClass; $b = new MyClass; $c = 'MyClass'; $d = 'NotMyClass'; var_dump($a instanceof $b); // $b is an object of class MyClass var_dump($a instanceof $c); // $c is a string 'MyClass' var_dump($a instanceof $d); // $d is a string 'NotMyClass' ?>

The above example will output:

bool(true) bool(true) bool(false)

instanceof does not throw any error if the variable being tested is not an object, it simply returns false . Constants, however, were not allowed prior to PHP 7.3.0.

Example #6 Using instanceof to test other variables

 $a = 1; $b = NULL; $c = imagecreate(5, 5); var_dump($a instanceof stdClass); // $a is an integer var_dump($b instanceof stdClass); // $b is NULL var_dump($c instanceof stdClass); // $c is a resource var_dump(FALSE instanceof stdClass); ?>

The above example will output:

bool(false) bool(false) bool(false) PHP Fatal error: instanceof expects an object instance, constant given

As of PHP 7.3.0, constants are allowed on the left-hand-side of the instanceof operator.

Example #7 Using instanceof to test constants

 var_dump(FALSE instanceof stdClass); ?>

Output of the above example in PHP 7.3:

As of PHP 8.0.0, instanceof can now be used with arbitrary expressions. The expression must be wrapped in parentheses and produce a string.

Example #8 Using instanceof with an arbitrary expression

 class ClassA extends \stdClass <> class ClassB extends \stdClass <> class ClassC extends ClassB <> class ClassD extends ClassA <> function getSomeClass( ): string < return ClassA::class; > var_dump(new ClassA instanceof ('std' . 'Class')); var_dump(new ClassB instanceof ('Class' . 'B')); var_dump(new ClassC instanceof ('Class' . 'A')); var_dump(new ClassD instanceof (getSomeClass())); ?>

Output of the above example in PHP 8:

bool(true) bool(true) bool(false) bool(true)

The instanceof operator has a functional variant with the is_a() function.

Источник

Оцените статью