This tutorial contains some parts of tuorial
Object Oriented PHP - Class and object
Object Oriented PHP - Interface and abstractor
Object Oriented PHP - Constructor and destructor
Object Oriented PHP - Inheritance
Object Oriented PHP - Visivility
Object Oriented PHP - Some common keywords
some magic words in php
Object Inheritance
Probably the greatest feature of the PHP OOP model is Inheritence. Inheritence is the ability of php to extend classes (child classes) that inherit the charactaristics of the parent class. The resulting object of an extend class has all the charactaristics of the parent class, plus whatever is in the new child, or extended class. In this instance we will extend the vehicle class and add a motorcyle. A motorcycle is still a vehicle and shares many of the same attributes such as price, drive etc. But a motorcycle has some unique features that a car does not.
Syntax of object Inheritance in php. the keyword "extends" denoted that it inherited other class. In the below example the "Bike" class inherited the class "Vehicle". In the inherited example we define that the child class is inherited the parent class.
Syntax of object Inheritance in php. the keyword "extends" denoted that it inherited other class. In the below example the "Bike" class inherited the class "Vehicle". In the inherited example we define that the child class is inherited the parent class.
<?php class Vehicle { } class Bike extends Vehicle{ //inherited the Vehilce class } ?>
<?php class Vehicle { public $brand="Motorola"; private $color="Black"; protected $price="$1200"; function shop(){ echo "Welcome to ".$this->color." vehicle"; } public function showPrice(){ echo "Price : ".$this->price; } private function showBrand(){ echo "Brand : ".$this->brand; } } class Bike extends vehicle{ public $size="120M"; public $model="ds20"; function bikeInfo(){ parent::shop(); //call the parent function in child class echo "Model : ".$this->model; echo "Price : ".$this->price; echo "Color : ".$this->color; // color is the properties of Vehicle class which are private. So outside the vehilce class can not access. echo "Brand : ".$this->brand; } } $obj1 = new Vehicle(); $obj = new Bike(); echo "info : ".$obj->bikeInfo() ?>See a clear and large exmple where classes are inherited from another class
<?php class Vehicle { public $brand="Motorola"; public $color="Black"; public $price="$1200"; function shop(){ echo "Welcome to ".$this->color." vehicle"; } public function showPrice(){ echo "Price : ".$this->price; } public function showBrand(){ echo "Brand : ".$this->brand; } } class Bike extends vehicle{ public $size="120M"; public $model="ds20"; public function bikeInfo(){ parent::shop(); //call the parent function in child class echo "Buy a Bike.\n"; echo "Model : ".$this->model; echo "Price : ".$this->price; echo "Color : ".$this->color; echo "Brand : ".$this->brand; } } class car extends vehicle{ public $size="220M"; public $model="SS66"; public function carInfo(){ parent::shop(); //call the parent function in child class echo "Buy a Car.\n"; echo "Model : ".$this->model; echo "Price : ".$this->price; echo "Color : ".$this->color; echo "Brand : ".$this->brand; } } class pazero extends car{ public $speed="160km"; public $range="100"; public function pazeroInfo(){ echo "Buy a Pazero.\n"; parent::carInfo(); //call the parent function in child class echo "Speed : ".$this->speed; echo "Range : ".$this->range; } } $obj1 = new Vehicle(); $obj2 = new Bike(); $obj3 = new car(); $obj6 = new pazero(); echo "info : ".$obj6->pazeroInfo() ?>
No comments:
Post a Comment