mardi 31 mai 2016

How to handle nested dynamics objects in PHP?

I have a foreach that instances several kind of classes and this must be nested. They all extends of the same Abstract class.

<?php
$A1 = class A extends Parent { $type = 1 }
$A2 = class A extends Parent { $type = 2 }
$B = class B extends Parent {}
$A3 = class A extends Parent { $type = 2 }
$C = class C extends Parent {}

// foreach ([$A1, $A2, $B, $A3, $C])

But $A1 can not be nested, $A2 can only be nested by $A1, $B can be nested by $A1 and $A2 but should be nested by $A2 because it's the previous object that can nest it, $A3 should be nested to $A1, etc.

I don't need to handle any specific case but all the cases.

How should I handle this nesting? I was thinking about defining a function in the class that does the foreach, or maybe in the Parent class, that have all this cases defined inside.

Aucun commentaire:

Enregistrer un commentaire