Beispiel #1
0
        print $this->getValue($dd) . "\n";
    }
}
class ConcreteClass1 extends AbstractClass
{
    protected function getValue($dd)
    {
        return "ConcreteClass1 " . $dd . ' line 21 ';
    }
    public function prefixValue($prefix)
    {
        return "{$prefix}ConcreteClass1";
    }
}
class ConcreteClass2 extends AbstractClass
{
    public function getValue($dd)
    {
        return "ConcreteClass2 " . $dd . ' line 32 ';
    }
    public function prefixValue($prefix)
    {
        return "{$prefix}ConcreteClass2";
    }
}
$class1 = new ConcreteClass1();
$class1->printOut();
echo $class1->prefixValue('FOO_') . "\n";
$class2 = new ConcreteClass2();
$class2->printOut();
echo $class2->prefixValue('FOO_') . "\n";
    // Force Extending class to define this method
    protected abstract function getValue();
    protected abstract function prefixValue($prefix);
    // Common method
    public function printOut()
    {
        print $this->getValue() . "\n";
    }
}
class ConcreteClass1 extends AbstractClass
{
    // This method is not implemented on AbstractClass
    // So, if I don't implement this method here, it will generate an error
    protected function getValue()
    {
        return "ConcreteClass1";
    }
    // Same here. This method is only assign on AbstractClass
    // So it forces me to implement on Extending class (ConcreteClass1)
    public function prefixValue($prefix)
    {
        return "{$prefix}ConcreteClass1";
    }
}
$concreteClass1 = new ConcreteClass1();
// echo $concreteClass1->prefixValue('Foo'); // FooConcreteClass1
// In this case, I'm using a method from Abstract (Parent) class from my extended (Children class)
// So, with Abstract it's possible to pass to childres implemented methods but only assigned it's
// forced to implement on it.
echo $concreteClass1->printOut();
// ConcreteClass1