-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_4.php
More file actions
141 lines (113 loc) · 2.33 KB
/
Copy pathcontainer_4.php
File metadata and controls
141 lines (113 loc) · 2.33 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
*=============================================================
* <container_4.php>
* Object Oriented Programming: Dependency Injection Container in PHP
* Using PHP magic functions __set() and __get()
* @author Muhammad Anwar Hussain<anwar_hussain.01@yahoo.com>
* Created on: 15th June 2019
* ============================================================
*/
trait Trademarks
{
public function logo($logo)
{
echo get_class($this) . '\'s '. ucfirst($logo) .'<br>';
}
public function hallmark($hmark)
{
echo get_class($this) . '\'s '. ucfirst($hmark) .'<br>';
}
}
trait Settings
{
protected $data = [];
public function __construct(array $data = [])
{
$this->data = $data;
}
public function execute($key, $subkey)
{
$value = $this->data[$key][$subkey];
$this->$value($value);
}
}
class Foo
{
use Trademarks;
use Settings;
}
class Bar
{
use Trademarks;
use Settings;
}
class Baz
{
use Trademarks;
use Settings;
}
class Registration
{
protected $data = [];
function __construct(array $data = [])
{
$this->data = $data;
}
public function register($key, $subkey)
{
$array = $this->data;
array_shift($array);
return new $this->data[$key][$subkey]($array);
}
}
class Container
{
protected $data = [];
static protected $instance;
public function __set($key, $value)
{
$this->data[$key] = $value;
}
public function __get($key)
{
return $this->data[$key];
}
public function getInstance($key)
{
if (!isset(self::$instance))
{
$array = $this->data['data'];
array_shift($array);
self::$instance = new $this->data['data'][$key]($array);
}
return self::$instance;
}
}
[$register, $companies, $trademarks] = ['register', 'company', 'trademark'];
$data = array( $register => 'Registration',
$companies => array(
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
),
$trademarks => array(
'logo' => 'logo',
'hmark' => 'hallmark',
),
);
$c = new Container();
$c->data = $data;
$r = $c->getInstance($register);
$ckeys = array_keys($data[$companies]); // $ckeys = ['foo', 'bar', 'baz'];
$mkeys = array_keys($data[$trademarks]); // $mkeys = ['logo', 'hmark'];
for($i = 0; $i < count($ckeys); $i++)
{
$cls = $r->register($companies, $ckeys[$i]);
for($j = 0; $j < count($mkeys); $j++)
{
$cls->execute($trademarks, $mkeys[$j]);
}
echo '<br>';
}
?>