php-basics-revision [topic-name: inheritance,abstract class,Interfaces ]
inheritance:
when a class derives from another class , parent class and child class relationship between them , relation stablished with a keyword named
"extended" , child extends parent's public and protected properties, can modify with own setter methods can retrieve property values using getter methods...example
<?php
//parent class
class Fruit
{
public $name;
protected $color; //protected
private $weight; //private
//setting values of $name and $weight
public function __construct($name, $color)
{
$this->name = $name;
$this->color = $color;
}
//parents method printing value of $name and $color
public function intro()
{
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
//now here is the child method Apple
//Which extends parent class Fruit
//with extends keyword
class Apple extends Fruit{
//this a class from children class
public function message(){
echo "Am I a fruit or a berry? ";
}
// setter method in child class but it can
//set the value to parent class properties
public function set_color($color){
$this->color = $color ;
}
//getter method can retieve properties of parent class
public function get_color(){
return $this->color ;
}
// getter method it can not retieve properties from
//parent class because of access modifier is private
//private properties only can be access from defind class
//here private $weight defind in parent class Fruit
//it can be only accessable from parent class Fruit
public function get_weight(){
return $this->weight ;
}
}
//instance from fruite class
// mango instance have access of all
//properties and methods inside Fruite class
//access area of mango instance is
// *all properties
// *contructor method
// *intro method
$mango = new Fruit('mango','orange');
// $mango->intro() ;
//here is the child instance apple
// apple instance have all access of parent class
// public and protected properties
// child instance apple cannot access private properties
// private $weight of parent class
$apple = new Apple('apple','green') ;
$apple->message() ;
$apple->intro() ;
$apple->set_color('indigo');
echo($apple->get_color() );
echo($apple->get_weight() );
?>
Overriding Inherited Methods(method-overriding):
Inherited methods can be overridden by redefining the same methods (use the same name) in the child class.
class Fruit
{
public $name;
protected $color;
public function __construct($name, $color)
{
$this->name = $name;
$this->color = $color;
}
public function intro()
{
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Apple extends Fruit{
private $weight ;
public function message(){
echo "Am I a fruit or a berry? ";
}
public function set_color($color){
$this->color = $color ;
}
public function get_color(){
return $this->color ;
}
public function set_weight($weight){
$this->weight = $weight ;
}
public function get_weight($weight){
return $this->weight ;
}
//here is the method named intro which is overriden in
//this child method and output is with width property
public function intro(){
echo "The fruit is over ridden {$this->name} and the color is {$this->color}. and weight is {$this->weight}";
}
// public function get_weight(){
// return $this->weight ;
// }
}
$mango = new Fruit('mango','orange');
// $mango->intro() ;
$apple = new Apple('apple','green') ;
$apple->message() ;
$apple->intro() ; //will return intro of child method
echo($apple->get_color() ); //will return default value 'green'
$apple->set_color('indigo'); // will set 'indigo' to color
echo($apple->get_color() ); // will return 'indigo'
$apple->set_weight(1000) ; // will set private property value weight
$apple->intro() ; // will return children overrided method
echo($apple->get_color() ); // will return setted color indigo , if
//we don't set color then it will return default color value 'green'
if we write final method in parent class and want to override from child class then it's can not be possible .
Abstract Class and Method : The behaviour of Abstract classes and methods are when a parent class named or declared or initialize a method and extended child class/classes implement or write functionality of the initialized method.
An Abstract class is a class that has at least one abstract method ,
An Abstract method is a method that declared but not implemented in initialized class.
abstract class Car //abstract class with abstract keyword
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
//defining abstract method and this method will
//return a string value
//we just initialize the abstract method named intro here
//we will implement all functionality as required in
// extended child classes
//here access modifier is public and function name is intro
//have no parament
//if intro function have any parameter then children class
//must insert same number of parament in intro class
//abstract protected function intro('parameter-1','parameter-2'): string;
abstract public function intro(): string;
}
//rules of implement abstract method in child class
//1. access modified must be same
//2. function name must be same
//3. number of parameter of method must be same
//child class-1 of abstract class Car
class Audi extends Car
{
//same access modified function name same , parameter number same
//abstract protected function intro('parameter-1','parameter-2'): string;
public function intro(): string
{
return "Choose German quality! I'm an $this->name!";
}
}
class Volvo extends Car {
public function intro() : string {
return "Proud to be Swedish! I'm a $this->name!";
}
}
class Citroen extends Car {
public function intro() : string {
return "French extravagance! I'm a $this->name!";
}
}
$a1 = new Audi('audi') ;
echo ($a1->intro()) ;
echo "\n";
$v1 = new Volvo('volvo') ;
echo ($v1->intro()) ;
echo "\n";
$c1 = new Citroen('citroen');
echo ($c1->intro());
echo "\n";
another example or use case of abstract class focused on abstract methods number of arguments
abstract class ParentClass
{
// Abstract method with an argument
abstract protected function prefixName($name);
}
class ChildClass extends ParentClass {
// The child class may define optional arguments that are not in the parent's abstract method
public function prefixName($name, $separator = ".", $greet = "Dear") {
if ($name == "John Doe") {
$prefix = "Mr";
} elseif ($name == "Jane Doe") {
$prefix = "Mrs";
} else {
$prefix = "";
}
return "{$greet} {$prefix}{$separator} {$name}";
}
}
$class = new ChildClass;
echo $class->prefixName("John Doe");
echo "\n";
echo $class->prefixName("Jane Doe");
echo "\n";
Interfaces: interfaces allows you to specify what methods a class should implement.
A class should implements which methods interface allows.
when one or more classes use the same interfece , its referred to as polymorphism
polymorphism is same named methods but different behaviour inside different classes
interface Gadget{
public function needElecticity();
}
class Computer implements Gadget{
public function needElecticity()
{
echo "220 volt per hour\n";
}
}
class Notebook implements Gadget{
public function needElecticity()
{
echo "170 volt per hour\n";
}
}
class Phone implements Gadget{
public function needElecticity()
{
echo "110 volt per hour\n";
}
}
$g1 = new Computer();
$g1->needElecticity();
$n1 = new Notebook();
$n1->needElecticity();
$p1 = new Phone();
$p1->needElecticity();
echo "\n";
difference between Interface and Abstract Class:
1.interface cannot have properties but Abstract method have properties .
2.All Interface methods must be public where Abstract methods can be public or protected
3.A single Child class can implements an Interface and also Extends a Parent class both in same time.