function run_component_proba() { // Create a tree structure $root = new Composite("root"); $root->Add(new Leaf("Leaf A")); $root->Add(new Leaf("Leaf B")); $comp = new Composite("Composite X"); $comp->Add(new Leaf("Leaf XA")); $comp->Add(new Leaf("Leaf XB")); $root->Add($comp); $root->Add(new Leaf("Leaf C")); // Add and remove a leaf $leaf = new Leaf("Leaf D"); $root->Add($leaf); $root->Remove($leaf); // Recursively display tree $root->Display(); }
return $this->items[$component->GetName()]; } return null; } public function GetName() : string { return $this->name; } public function SetName(string $value) { $this->name = $value; } } $product = new Composite("Product"); $a = new Composite("Part A"); $b = new Composite("Part B"); $c = new Composite("Part C"); $a->Add(new Single("Sub Part A-1")); $a->Add(new Single("Sub Part A-2")); $a->Add(new Single("Sub Part A-3")); $a->Add(new Single("Sub Part A-4")); $b->Add(new Single("Sub Part B-1")); $b->Add(new Single("Sub Part B-2")); $c->Add(new Single("Sub Part C-1")); $c->Add(new Single("Sub Part C-2")); $c->Add(new Single("Sub Part C-3")); $product->Add($a); $product->Add($b); $product->Add($c); echo $product->Display(0);