forked from assembler-institute/oop-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-abstract-classes.php
60 lines (49 loc) · 1.86 KB
/
12-abstract-classes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 12 - Abstract classes */
// An abstract class cannot be instantiated, only inherited. We can declare an abstract class with the keyword abstract.
// When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child.
const APPNAME = 'ASSEMBLER APP';
class Internet
{
const SERVER = '122.23.4.5';
public static function connectInternet()
{
return "connecting to " . self::SERVER . "...";
}
}
// We convert Mobile class into an abstract class
abstract class Mobile
{
public $name;
protected $chipset;
protected $internalMemory;
private $imei;
public function __construct($name, $chipset, $internalMemory, $imei)
{
$this->name = $name;
$this->chipset = $chipset;
$this->internalMemory = $internalMemory;
$this->imei = $imei;
echo "+ CREATED " . $this->name . " WITH " . $this->internalMemory . " INTERNAL MEMORY +<br>";
}
public function runMobileApp()
{
return $this->name . " RUNS " . APPNAME . " : " . Internet::connectInternet();
}
}
class Blackberry extends Mobile
{
public $keyboard;
public function __construct($name, $chipset, $internalMemory, $imei, $keyboard)
{
parent::__construct($name, $chipset, $internalMemory, $imei);
$this->keyboard = $keyboard;
}
}
$blackberry = new BlackBerry('BlackBerry', 'ARM', 1, '99966688555', 'qwerty');
echo $blackberry->runMobileApp();
// We cannot instantiate an abstract class by itself!!
$samsung = new Mobile('Samsung s20', 'Exynos', 128, '000111222333'); // Fatal error: Uncaught Error: Cannot instantiate abstract class Mobile