oops in php
Refer to an Instance in php
- Outside the class: $variable->
- Inside the class: $this->
File:Refer_instance.php
<?php
class Student{
var $first_name;
var $last_name;
var $country='none';
function say_hello()
{
return 'hello world';
}
function full_name()
{
return $this->first_name."".$this->last_name;
}
}
$student1 = new Student;
$student1->first_name='Dilip';
$student1->last_name='Kumar';
$student2 = new Student;
$student2->first_name='Divya';
$student2->last_name='Sree';
echo $student1->full_name()."<br>";
echo $student2->full_name()."<br>";
echo $student1->say_hello().'<br>';
echo $student2->say_hello().'<br>';
$class_methods= get_class_methods('Student');
echo "class_methods".implode(',',$class_methods).'<br>';
if(method_exists('student','say_hello'))
{
echo 'method say_hello() exists in student class';
}
else
{
echo 'method say_hello()not exists in student class';
}
?>
Output:
Dilip Kumar
Divya Sree
Hello world
Hello world
class methods:say_hello,full_name
method say_hello exists in Student class