-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy path08-inheritance-solution.php
70 lines (58 loc) · 1.96 KB
/
08-inheritance-solution.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
61
62
63
64
65
66
67
68
69
70
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 08 - Inheritance solution */
// Let's apply inheritance in latest example
class Mobile
{
public $name;
public $chipset;
public $internalMemory;
public function __construct($name, $chipset, $internalMemory)
{
// when we create a constructor we can add arguments and then initialize the properties with those argument values
$this->name = $name;
$this->chipset = $chipset;
$this->internalMemory = $internalMemory;
}
public function getName()
{
return $this->name;
}
public function getChipset()
{
return $this->chipset;
}
public function getInternalMemory()
{
return $this->internalMemory;
}
public function getMobileDetails()
{
return "Name: $this->name, Chipset: $this->chipset, Internal Memory: $this->internalMemory";
}
}
// When you extend a class, the subclass inherits all of the public and protected methods from the parent class.
class Blackberry extends Mobile
{
public $keyboard;
// in php we use __construct to tell our class that this is the constructor method
public function __construct($name, $chipset, $internalMemory, $keyboard)
{
// we use same constructor as father class with parent keyword and double colon
parent::__construct($name, $chipset, $internalMemory);
// and add new arguments necessary for the new son class
$this->keyboard = $keyboard;
}
//new method for getting keyboard type
public function getKeyboard()
{
return $this->keyboard;
}
}
$samsung = new Mobile('Samsung s20', 'Exynos', 128);
$blackberry = new BlackBerry('BlackBerry', 'ARM', 1, 'qwerty');
echo $blackberry->getName();
echo "\n";
echo $blackberry->getMobileDetails();