Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface Element{
- function prints();
- }
- class A implements Element{
- function prints(){
- echo 'A element';
- }
- }
- class B implements Element{
- function prints(){
- echo 'B element';
- }
- }
- class C implements Element{
- function prints(){
- echo 'C element';
- }
- }
- class Composite implements Element{
- private $list=array();
- function prints(){
- foreach($this->list as $obj){
- if($obj instanceOf Element)$obj->prints();
- else echo $obj;
- }
- }
- function add($e){
- $this->list[]=$e;
- }
- }
- $c= new Composite();
- $c2= new Composite();
- $c2->add(1);
- $c2->add(new C());
- $c->add(new A());
- $c->add(new B());
- $c2->add($c);
- $c2->prints();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement