oops in php


when a new class takes on the properties and methods of an existing class.

Why would we want to do that

Well using class inheritance help us to

  1. Organize our code
  2. Prevent repetition
  3. Simplify maintenance
  4. Prevent inconsistencies and bugs

Class Inheritance

  1. Behaviors of a class are shared with subclasses
  2. Add new behaviors to paent and all sub classes gain them
  3. subclasses can override parent behaviors
  4. subclass can extend parent behaviors

Define a Subclass

class Parent{
class Subclass extends Parent{

Functions for Inheritance:

  1. get_parent_class($mixed)
  2. is_subclass_of($mixed,$string)
  3. class_parents($mixed)
class User{

var $first_name;
var $last_name;
var $username;

function full_name(){
return $this->first_name."".$this->last_name;
class Customer extends User{
$u = new User;

$c = new Customer ;

echo $u->full_name().'<br>';
echo $c->full_name().'<br>';

echo get_parent_class($u).'<br>';
echo get_parent_class($c).'<br>';

echo 'instance is a subclass of User.<br>';
echo implode(',',$parents).'<br>';


Saraneys maharaj
madhu v

instance is a subclass of User