A class that has minimum one abstract method is called as abstract class. An abstract class is created with abstract keyword and it cannot be instantiated by itself. Abstract methods can only declare the method's signature; they cannot define the actual implementation. An abstract class can also have both abstract and non abstract methods.
Example 1: An abstract class must be declared with abstract keyword and cannot be instantiated on itself
<?php
abstract class Vehicle
{
abstract function getBrand(): string;
abstract function getModel(): string;
}
class Car extends Vehicle
{
public $brand;
public $model;
public function __construct($brand, $model)
{
$this->brand = $brand;
$this->model = $model;
}
public function getBrand(): string
{
return $this->brand;
}
public function getModel(): string
{
return $this->model;
}
}
$obj = new Car('TATA', 'Safari');
$properties = array(
'Brand' => $obj->getBrand(),
'Model' => $obj->getModel(),
);
print_r($properties);
/* OUTPUT
Array
(
[Brand] => TATA
[Model] => Safari
)
*/
?>
Example 2: A class that extends an abstract class must implements all abstract methods inherited from abstract class or itself declread as abstract.
<?php
abstract class Vehicle
{
abstract function getBrand(): string;
abstract function getModel(): string;
}
abstract class Car extends Vehicle
{
abstract function getName(): string;
}
class MyCar extends Car
{
public $brand;
public $model;
public $name;
public function __construct($brand, $model, $name)
{
$this->brand = $brand;
$this->model = $model;
$this->name = $name;
}
public function getBrand(): string
{
return $this->brand;
}
public function getModel(): string
{
return $this->model;
}
public function getName(): string
{
return $this->name;
}
}
$obj = new MyCar('Toyota', 'Innova', 'Innova Crysta');
$properties = array(
'Brand' => $obj->getBrand(),
'Model' => $obj->getModel(),
'Name' => $obj->getName(),
);
print_r($properties);
/* OUTPUT
Array
(
[Brand] => Toyota
[Model] => Innova
[Name] => Innova Crysta
)
*/
?>
Example 3: An abstract class can also have constructor in it as shown in below example.
<?php
abstract class Vehicle
{
public $brand;
public $model;
public function __construct($brand, $model)
{
$this->brand = $brand;
$this->model = $model;
}
abstract function getBrandAndModel(): string;
}
class Car extends Vehicle
{
public function getBrandAndModel(): string
{
return "Hi, I am '$this->model' from '$this->brand'.";
}
}
$details = array();
$obj1 = new Car('Toyota', 'Innova');
array_push($details, $obj1->getBrandAndModel());
$obj2 = new Car('Tata', 'Safari');
array_push($details, $obj2->getBrandAndModel());
$obj3 = new Car('Hyundai', 'Venue');
array_push($details, $obj3->getBrandAndModel());
print_r($details);
/* OUTPUT
Array
(
[0] => Hi, I am 'Innova' from 'Toyota'.
[1] => Hi, I am 'Safari' from 'Tata'.
[2] => Hi, I am 'Venue' from 'Hyundai'.
)
*/
?>
An Interface allows you to create programs that specifies which methods a class must implement, without defining how those methods are implemented.
Magic Methods are special types of methods in PHP that allows you to perform certain special actions. These methods start with a double underscore (__).
Traits in PHP, are a medium of implementing code reuse and allow us to reuse sets of functions freely in several independent classes.