-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy path16-namespaces.php
116 lines (96 loc) · 3.28 KB
/
16-namespaces.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================
/* File 16 - Namespaces */
// Namespaces are designed to solve two problems we encounter when creating re-usable code elements such as classes or functions:
// - Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
// - Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.
// imagine we need an external library for implementing some tasks in our mobile classes
// inside mobileLibs there's already an interface named iMobileApp so if we run this php we will get an error:
// Fatal error: Cannot declare interface iMobileApp, because the name is already in use in D:\xampp\htdocs\assembler-school\oop-basics\mobileLibs.php on line 4
// we have to add a namespace in mobileLibs
include 'mobileLibs.php';
interface iMobileApp
{
public static function showSplashScreen();
public static function getData();
public static function showData();
}
class AssemblerApp implements iMobileApp
{
const APPNAME = 'ASSEMBLER APP';
public static $data;
public static function showSplashScreen()
{
echo "@@@ " . self::APPNAME . " @@@";
}
public static function getData()
{
self::$data = self::APPNAME . " " . Internet::connectInternet();
}
public static function showData()
{
echo self::$data;
}
public static function exitApp()
{
echo "@@@ BYE " . self::APPNAME . " BYE @@@";
}
}
class Internet
{
const SERVER = '122.23.4.5';
public static function connectInternet()
{
return "connecting to " . self::SERVER . "...";
}
}
abstract 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 runAssemblerApp()
{
echo "<br>";
AssemblerApp::showSplashScreen();
echo "<br>";
AssemblerApp::getData();
AssemblerApp::showData();
echo "<br>";
AssemblerApp::exitApp();
}
public function runAssemblerAppLib()
{
// We use the namespace for calling our library methods
Lib\AssemblerApp::showSplashScreen();
echo "<br>";
Lib\AssemblerApp::getData();
Lib\AssemblerApp::showData();
echo "<br>";
Lib\AssemblerApp::exitApp();
}
}
class Blackberry extends Mobile
{
public $keyboard;
public function __construct($name, $chipset, $internalMemory, $imei, $keyboard)
{
parent::__construct($name, $chipset, $internalMemory, $imei);
$this->keyboard = $keyboard;
}
}
$blackberry = new BlackBerry('BlackBerry', 'ARM', 1, '99966688555', 'qwerty');
echo $blackberry->runAssemblerApp();
echo "<br><br>";
echo $blackberry->runAssemblerAppLib();