Loading...

oops in php

Class Methods in php

  1. functions to perform  object actions
  2. Non-oop:" functions",Oop:"methods"
  3. Define inside classes like regular functions

Example:

<?php

class Person{
var $first_name;
var $last_name;
function say_hello()
{
 return "Hello worls";
}

}
$customer= new Person;
$customer->first_name='Harsha';

echo $customer->first_name;//Harsha
echo $customer->say_hello();//Hello world

Functions for methods:

  1. get_class_methods($mixed):-Gets the class methods' names
  2. method_exists($mixed,$string):-Checks if the class method exists

File:class_methods.php

<?php

class Student{
var $first_name;
var $last_name;
var $country='none'
function say_hello()
{
 return "Hello worls";
}

}
$student1= new Student;
$student1->first_name='Sai';
$student1->last_name='Lakshmi';

$student2= new Student;
$student2->first_name='chandana';
$student2->last_name='gidugu';

echo $student1->first_name."".student1->last_name."<br>";
echo $student2->first_name."".student2->last_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(methods_exists('Student','say_hello'))
{
 echo 'method say_hello() exists in student class.'<br>';
}
else
{
  echo 'method say_hello() does not exists in student class.'<br>';
}


Output:

Sai Lakshmi
Chandana Gidugu
Hello world
Hello world
class methods:say_hello
method say_hello() exists in Student class