質問編集履歴

1

質問が曖昧だったので修正しました

2018/06/06 03:10

投稿

msx2
msx2

スコア174

test CHANGED
File without changes
test CHANGED
@@ -128,7 +128,7 @@
128
128
 
129
129
  上記プログラムはなかり簡略化してしまったので間違っているかもしれませんが、
130
130
 
131
- やりたいこととして画像管理クラスが区分/重要度別に画像保持と取り出しの順序の管理です。
131
+ 目的は区分/重要度別に画像保持するこ取り出しの順序の管理です。
132
132
 
133
133
 
134
134
 
@@ -160,15 +160,151 @@
160
160
 
161
161
 
162
162
 
163
- 配列をオブジェクトにした場合、データへのアクセスは配列のようにキーを使うことができません。
164
-
165
- どの様な実装になるのでしょうか?
166
-
167
-
168
-
169
- 今の参照使った出力順の指定別の方法に置き換えることはできるでょうか?
163
+ 画像入れておく配列下記クラスと置き換えてみまた。
164
+
170
-
165
+ ```PHP
166
+
171
-
167
+ class ImageBox {
168
+
169
+ private $img_kbn;
170
+
171
+ private $importance;
172
+
173
+ private $images = [];
174
+
175
+
176
+
177
+ public function __construct($img_kbn,$importance){
178
+
179
+ $this->img_kbn = $img_kbn;
180
+
181
+ $this->importance = $importance;
182
+
183
+ }
184
+
185
+ public function add($image){
186
+
187
+ $images[] = $image;
188
+
189
+ }
190
+
191
+ public function get(){
192
+
193
+ return $images;
194
+
195
+ }
196
+
197
+ }
198
+
199
+ ```
200
+
201
+ 最初のプログラムは下記となります。
202
+
203
+ ```PHP
204
+
205
+ //画像管理クラス
206
+
207
+ class ImageManager {
208
+
209
+
210
+
211
+ //画像データ
212
+
213
+ private $_image_data = [];
214
+
215
+
216
+
217
+ //出力順序
218
+
219
+ private $_output_order;
220
+
221
+
222
+
223
+ public function __construct(){
224
+
225
+
226
+
227
+ $this->_image_data = [
228
+
229
+ new ImageBox('common','imp_high'),
230
+
231
+ new ImageBox('common','imp_middle'),
232
+
233
+ new ImageBox('common','imp_low'),
234
+
235
+ new ImageBox('individual','imp_high'),
236
+
237
+ new ImageBox('individual','imp_middle'),
238
+
239
+ new ImageBox('individual','imp_low'),
240
+
241
+ ];
242
+
243
+
244
+
245
+ //出力の順序を決める
246
+
247
+ $this->_output_order[] = $this->_image_data[0];
248
+
249
+ $this->_output_order[] = $this->_image_data[1];
250
+
251
+ $this->_output_order[] = $this->_image_data[2];
252
+
253
+ $this->_output_order[] = $this->_image_data[3];
254
+
255
+ $this->_output_order[] = $this->_image_data[4];
256
+
257
+ $this->_output_order[] = $this->_image_data[5];
258
+
259
+ }
260
+
261
+
262
+
263
+ public function get_images()
264
+
265
+ {
266
+
267
+ foreach($this->_output_order as $images)
268
+
269
+ {
270
+
271
+ foreach($images->get() as $img)
272
+
273
+ {
274
+
275
+ yield $img;
276
+
277
+ }
278
+
279
+ }
280
+
281
+ }
282
+
283
+ //画像を追加
284
+
285
+ public function add($img_kbn, $importance,$image)
286
+
287
+ {
288
+
289
+ //ここがわかりません
290
+
291
+ }
292
+
293
+ ```
294
+
295
+ ImageManagerクラスのaddメソッドについて質問ですが
296
+
297
+
298
+
299
+ 引数で与えられた$img_bknと$importanceから該当するImageBoxオブジェクトを選択して画像を追加したいのですが、どのようにImageBoxオブジェクトを探すのがいいでしょうか?
300
+
301
+
302
+
303
+ foreachで回しながら1つ1つ確認する方法しか思いつきません。
304
+
305
+
306
+
307
+ これならキーでアクセスできる配列の方がわかりやすい感じがしますが、データのチェックなど配列ではできない処理を実装する予定なので配列はやめようと思います。
172
308
 
173
309
 
174
310