Skip to Content
PHPCODE
Inherirance and overriding in php
oops code / August 11, 2018

Inheritance and overriding in PHP

<?php
class TV
{
public $model;
public $volume;
function volumeUp()
{
$this->volume++;
}
function volumeDown()
{
$this->volume--;
} 
function __construct($m,$v)
{ 
$this->model=$m;
$this->volume=$v;
} 
}
class TvWithTimer extends TV
{
public $timer=true;
}
class plazmaTV extends TV
{
public $plazma=true;
}
$tv=new TvWithTimer('Tv with Timer',2);
$plazma=new plazmaTV('plazmaTV',3);
echo $plazma->model;
?>

Overriding in PHP

<?php
class TV
{
public $model;
public $volume;
function volumeUp()
{
$this->volume++;
}
function volumeDown()
{
$this->volume--;
}
function __construct($m,$v)
{
$this->model=$m;
$this->volume=$v;
}
}
class plazmaTV extends TV
{
//overriding
public $model='def';
function __construct()
{
}
}
$plazma=new plazmaTV;
$plazma->model='ddd';
echo $plazma->model;
?>
PHPCODE © 2024