-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy path06-constructors.php
34 lines (29 loc) · 1.35 KB
/
06-constructors.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
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 06 - Constructors & Destructors */
class Mobile
{
public $name;
public $chipset;
public $internalMemory;
// in PHP we use __construct to tell our class that this is the constructor method, it will be called automatically when an object of this class is created
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>";
}
// PHP will call __destruct at the end of the script, when the object is destructed or the script is stopped or exited.
function __destruct()
{
echo "- DESTROYED : " . $this->name . " includes a " . $this->chipset . " chipset and " . $this->internalMemory . "GB of internal memory -<br>";
}
}
// Now we can instantiate a mobile object passing arguments in the correct order
$modernMobile = new Mobile('Samsung s20', 'Exynos', 128);
$oldMobile = new Mobile('BlackBerry', 'ARM', 1);
echo "<br>";