@@ -58,9 +58,9 @@ class ObjectBox implements ContainerInterface
58
58
private bool $ callInit = true ;
59
59
60
60
/**
61
- * @var string
61
+ * @var string Will call init method on object create. Default is 'initObj()'
62
62
*/
63
- private string $ initMethod = 'init ' ;
63
+ private string $ initMethod = 'initObj ' ;
64
64
65
65
/**
66
66
* Created objects
@@ -70,9 +70,57 @@ class ObjectBox implements ContainerInterface
70
70
private array $ objects = [];
71
71
72
72
/**
73
- * Definitions for create objects
73
+ * Definitions for create objects.
74
+ *
75
+ * For definition format:
76
+ *
77
+ * - Closure(ObjectBox): object
78
+ * - Object and has __invoke()
79
+ * - string: an function name
80
+ * - array: callable array [class, method]
81
+ * - array: config array for create object
82
+ * - more, any type as config data.
83
+ *
84
+ * ```php
85
+ * [
86
+ * // array config of definition
87
+ * 'myObj' => [
88
+ * 'class' => MyObj::class,
89
+ * '__opt' => [
90
+ * 'callInit' => true, // call init method on object create. Default is true
91
+ * 'objType' => ObjectBox::TYPE_SINGLETON, // default is TYPE_SINGLETON
92
+ * 'argsForNew' => [
93
+ * 'foo' => 'bar',
94
+ * ],
95
+ * ],
96
+ * // props settings ...
97
+ * 'propName' => value,
98
+ * ],
99
+ * // use creator func
100
+ * 'myObj2' => [
101
+ * '__creator' => function (ObjectBox $box) {
102
+ * return new MyObj2();
103
+ * },
104
+ * '__opt' => [],
105
+ * // props settings ...
106
+ * 'propName' => value,
107
+ * ],
108
+ * // use [class, method] of definition
109
+ * 'myObj3' => [MyObj3::class, 'createObj3'],
110
+ * // function name of definition
111
+ * 'myObj4' => 'my_create_func',
112
+ * // Closure object
113
+ * 'myObj5' => function (ObjectBox $box) {
114
+ * return new MyObj5();
115
+ * },
116
+ * // callable object: has __invoke() method
117
+ * 'myObj6' => new Obj6Factory(),
118
+ * ]
119
+ *
120
+ * ```
74
121
*
75
122
* @var array
123
+ * @see createObject()
76
124
*/
77
125
private array $ definitions = [];
78
126
@@ -108,10 +156,11 @@ public function get(string $id): mixed
108
156
109
157
if (is_object ($ obj )) {
110
158
$ callInit = $ opt ['callInit ' ] ?? $ this ->callInit ;
159
+ $ initMth = $ this ->initMethod ;
111
160
112
161
// has init method in the object, call it.
113
- if ($ callInit && method_exists ($ obj , $ this -> initMethod )) {
114
- $ obj ->init ();
162
+ if ($ callInit && $ initMth && method_exists ($ obj , $ initMth )) {
163
+ $ obj ->$ initMth ();
115
164
}
116
165
117
166
// storage it on type is TYPE_SINGLETON
@@ -130,7 +179,7 @@ public function get(string $id): mixed
130
179
/**
131
180
* Create object from definition.
132
181
*
133
- * return options:
182
+ * return [obj, options], options:
134
183
*
135
184
* ```php
136
185
* [
@@ -188,7 +237,7 @@ protected function createObject(mixed $value): array
188
237
}
189
238
}
190
239
191
- // as config data.
240
+ // save as config data.
192
241
if ($ obj === null ) {
193
242
$ obj = $ value ;
194
243
}
@@ -209,7 +258,7 @@ protected function createObject(mixed $value): array
209
258
*
210
259
* ```php
211
260
* [
212
- * 'class' => class-string,
261
+ * 'class' => ' class-string' ,
213
262
* // '__creator' => callable(ObjectBox): object, // can also use creator func.
214
263
*
215
264
* // option for create object.
0 commit comments