-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_2.php
More file actions
107 lines (86 loc) · 1.69 KB
/
Copy pathcontainer_2.php
File metadata and controls
107 lines (86 loc) · 1.69 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
<?php
/**
*=============================================================
* <container_2.php>
* Object Oriented Programming: Dependency Injection Container in PHP
* Avoid Hard Coding Dependencies
* @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>';
}
}
class Foo
{
use Trademarks;
}
class Bar
{
use Trademarks;
}
class Baz
{
use Trademarks;
}
class Registration
{
protected $data = [];
function __construct(array $data = [])
{
$this->data = $data;
}
public function register($key)
{
$this->data['instantiate'] = new $this->data[$key];
}
public function execute($key)
{
$value = $this->data[$key];
$this->data['instantiate']->$value($value);
}
}
class Container
{
protected $data = [];
public function __construct(array $data = [])
{
$this->data = $data;
}
public function getInstance($key)
{
$array = $this->data;
array_shift($array);
return new $this->data[$key]($array);
}
}
$register = 'register';
$data = array(
$register => 'Registration',
'foo' => 'Foo',
'bar' => 'Bar',
'baz' => 'Baz',
'logo' => 'logo',
'hmark' => 'hallmark',
);
$c = new Container($data);
$r = $c->getInstance($register);
$keys = array_keys($data); // $ckeys = ['register', 'foo', 'bar', 'baz', 'logo', 'hmark'];
for($i = 1; $i < count($keys) - 2; )
{
$r->register($keys[$i++]);
for($j = 4; $j < count($keys); )
{
$r->execute($keys[$j++]);
}
echo '<br>';
}
?>