Design Patterns PHP - Decorator Java

1) What have we done?

We have interface ParentA and its implementation ParentAImpl


interface ParentA
{
    public function doA();
}

class ParentAImpl implements ParentA
{
    public function doA()
    {
        echo 'doing A ' . PHP_EOL;
    }
}




By analogy ParentB and ParentBImpl


interface ParentB
{
    public function doB();
}

class ParentBImpl implements ParentB
{
    public function doB()
    {
        echo 'doing B ' . PHP_EOL;
    }
}




The "Child" class(our "Decorator") implements interface "ProductA" in order to be transparent to Client (the type of Child must be the same as ProductA)
We achieve this interface by aggregating "ProductAImpl"

"Child" class inherits also after "ProductBImpl" in order to use its implementation.

In the other words "Child" decorating "ProductAImpl" means adds addition implementation.

class Child extends ParentBImpl implements ParentA
{

    /**
     * @var ParentA
     */
    private $parentA;

    function __construct(ParentA $parentA)
    {
        $this->parentA = $parentA;
    }

    public function doA()
    {
        $this->parentA->doA();
    }
}




In method "doA" in "Child" class we could easily besides call method "doA" add some other code. So we could decorating by adding another methods like here from
ProductBImpl but also we can modify behaviour of decorated object.


2) What is the final result ?

The final result is exactly the same as we want. Used in "Child" class both implementation od "ProductAImpl" as "ProductBImpl".