⊗ppOpAdOI 24 of 107 menu

Determining Object's Class Membership in OOP in PHP

Now we will study the instanceof operator. This operator is used to determine whether the current object is an instance of the specified class.

Let's look at an example. Suppose we have some two classes:

<?php // First class: class Class1 { } // Second class: class Class2 { } ?>

Let's create an object of the first class:

<?php $obj = new Class1; ?>

Let's check the membership of the object from the variable $obj to the first class and the second:

<?php // Will output true, because the object belongs to the Class1 class: var_dump($obj instanceof Class1); // Will output false, because the object does NOT belong to the Class2 class: var_dump($obj instanceof Class2); ?>

Make a Employee class with public properties name (name) and salary (salary).

Make a Student class with public properties name (name) and scholarship (scholarship).

Create 3 objects of each class and in an arbitrary order write them into the array $arr.

Loop through the $arr array and output a column of names of all employees to the screen.

In a similar way, output a column of names of all students to the screen.

Loop through the $arr array and use it to find the sum of employees' salaries and the sum of students' scholarships. After the loop, output these two numbers to the screen.

byenru