PHP instanceof

In this chapter you will learn:

  1. What is PHP instanceof
  2. Syntax for PHP instanceof
  3. Example - How to use PHP instanceof

Description

PHP instanceof is an operator. instanceof returns true if the object on the lefthand side is of the same class, or a descendant of, the class given on the righthand side.

You can also use the instanceof keyword to check whether an object implements an interface.

Syntax

PHP instanceof has the syntax of.

$varaibleName instanceof ClassName

Example

For example, given the code &aBook = new ComputerBook;.


<?PHP/* j  av  a  2  s. c  om*/
class Person {
}
$bill = new Person();

if ($bill instanceof Person) { 
   print("Is a person");
}
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Why do we need PHP serialize()
  2. Syntax for PHP serialize()
  3. Example - serialize a class
Home » PHP Tutorial » PHP Class Functions
PHP class_exists() function
PHP get_class() function
PHP get_declared_classes() function
PHP get_declared_interfaces() function
PHP get_defined_functions() function
PHP instanceof
PHP serialize() function