PHP Class Operators

Lecture



The instanceof operator is used to determine whether the current object is an instance of the specified class.

The instanceof operator was added in PHP5 . Previously, the is_a () function was used, which is currently not recommended for use; it is more preferable to use the instanceof operator.

<?php
class A { }
class B { }

$thing = new A;
if ($thing instanceof A) {
echo 'A';
} if ($thing instanceof B) {
echo 'B';
}
?>

Since the $ thing object is an instance of class A , and not at all B , only the first block based on class A will be executed:

A

See also the description of the get_class () and is_a () functions.

created: 2016-01-25
updated: 2021-03-13
132387



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)