forked from assembler-institute/oop-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-inheritance-problem.php
67 lines (54 loc) · 2.03 KB
/
07-inheritance-problem.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
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 07 - Why Inheritance? */
// Inheritance is another basic principle of OOP
class Samsung
{
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;
echo "+ " . $this->name . " CREATED +<br>";
}
function __destruct()
{
echo "- DESTROYED : " . $this->name . " includes a " . $this->chipset . " chipset and " . $this->internalMemory . "GB of internal memory -<br>";
}
}
// We need a class for mobiles with extra properties and methods that won't have every mobile
// For example we could need a class for a mobile device with physical keyboard so we create a new one
class Blackberry
{
public $name;
public $chipset;
public $internalMemory;
public $keyboard;
public function __construct($name, $chipset, $internalMemory, $keyboard)
{
$this->name = $name;
$this->chipset = $chipset;
$this->internalMemory = $internalMemory;
$this->keyboard = $keyboard;
echo "+ " . $this->name . " CREATED +<br>";
}
//new method for getting keyboard type
public function getKeyboard()
{
return $this->keyboard;
}
function __destruct()
{
echo "- DESTROYED : " . $this->name . " includes a " . $this->chipset . " chipset and " . $this->internalMemory . "GB of internal memory. It uses " . $this->keyboard . " Keyboard -<br>";
}
}
$samsung = new Samsung('Samsung s20', 'Exynos', 128);
$blackberry = new BlackBerry('BlackBerry', 'ARM', 1, 'qwerty');
echo "<br>";
// Seems that we are repeating too much code...