forked from assembler-institute/oop-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10-static.php
More file actions
57 lines (46 loc) · 1.57 KB
/
10-static.php
File metadata and controls
57 lines (46 loc) · 1.57 KB
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
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 10 - Static Methods */
// static methods can be called without creating an instance of the class and are declared with the static keyword
class Internet
{
public static $company = "orange";
public static function connectInternet()
{
return "connecting to the internet...";
}
// self word must be used to call static elements inside the same class
public function __construct()
{
echo self::connectInternet();
}
}
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 connectMobileInternet()
{
// we can call static methods without instantiating directly with class name and double colon
return $this->name . " : " . Internet::connectInternet();
}
}
// we can acces to static properties without instanciating the class
echo "<br>";
$company = Internet::$company;
echo $company;
echo "<br><br>";
$samsung = new Mobile('Samsung s20', 'Exynos', 128, '000111222333');
echo $samsung->connectMobileInternet();