oops in php
Instance in php
A single object created from a class definition is called Instance.Objects will be created by useing new keyword
<?php
Class Person{
}
$person1 =new Person;//1st instance
$person2 =new Person;//2nd instance
?>
Functions for Instance:
- get_class($object):-returns Class name of object
- is_a($object,$string):- checks matching object with string
File:class_instance.php
<?php
class Student{
}
$student1=new Student;
$student2=new Student;
echo get_class($student1).'<br>';
$class_names=['Teacher','Student','student'];
foreach($clas_names as $class_name){
if(is_a($student1,$class_name))
{
echo "student1 is a {$class_name}.<br>";
}
else
{
echo "student1 is not a {$class_name}.<br>";
}
}
?>
Output:
Student
student1 is not a Teacher
student1 is a Student
student1 is a Student