forked from assembler-institute/oop-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-overriding.php
50 lines (41 loc) · 1.42 KB
/
14-overriding.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
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 14 - Overriding */
// In function overriding, both parent and child classes should have
// same function name with and number of arguments. It is used to replace
// parent method in child class. The purpose of overriding is to change
// the behavior of parent class method. The two methods with the same
// name and same parameter is called overriding.
class iPhone
{
public $name;
public $internalMemory;
public function __construct($name, $internalMemory)
{
$this->name = $name;
$this->internalMemory = $internalMemory;
}
public function getName()
{
return $this->name;
}
public function getInternalMemory()
{
return $this->internalMemory;
}
}
// When you extend a class, the subclass inherits all of the public and protected methods from the parent class.
class iPhonePlus extends iPhone
{
//Here we are overriding this method adding extra features that the previous "getName" doesn't have.
public function getName()
{
return "¡The name of this iPhone is $this->name!";
}
}
$iPhone = new iPhone("iPhone X", "64");
echo $iPhone->getName() . "\n";
$iPhonePlus = new iPhonePlus("iPhone X Plus", "128");
echo $iPhonePlus->getName();