title: PHP: what is the use case of abstract class
body: <p>As per php.net documentation : </p>
  
  <blockquote>
    <p>When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature. This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ.</p>
  </blockquote>
  
  <p>In php.net they have shown the following example: </p>
  
  <pre><code>abstract class AbstractClass
  {
      // Force Extending class to define this method
      abstract protected function getValue();
      abstract protected function prefixValue($prefix);
  
      // Common method
      public function printOut() {
          print $this-&gt;getValue() . "\n";
      }
  }
  
  class ConcreteClass1 extends AbstractClass
  {
      protected function getValue() {
          return "ConcreteClass1";
      }
  
      public function prefixValue($prefix) {
          return "{$prefix}ConcreteClass1";
      }
  }
  
  class ConcreteClass2 extends AbstractClass
  {
      public function getValue() {
          return "ConcreteClass2";
      }
  
      public function prefixValue($prefix) {
          return "{$prefix}ConcreteClass2";
      }
  }
  
  $class1 = new ConcreteClass1;
  $class1-&gt;printOut();
  echo $class1-&gt;prefixValue('FOO_') ."\n";
  
  $class2 = new ConcreteClass2;
  $class2-&gt;printOut();
  echo $class2-&gt;prefixValue('FOO_') ."\n";
  </code></pre>
  
  <p>If I run this I get the below output:</p>
  
  <pre><code>ConcreteClass1 FOO_ConcreteClass1 ConcreteClass2 FOO_ConcreteClass2
  </code></pre>
  
  <p>Reading the documentation, my initial understanding was "parent classes in general can not access methods from their children if they are extend" and that's why we need abstract classes. However, when I tried the same example above with a little bit of tweak, removing all the abstractions (given below) I see the exact same output as the previous example. Therefore, why do we need abstract classes in PHP. Only use case I found is while implementing an interface in the parent class; I get error if I do not declare the parent as abstract. Is there other cases where I must or it is highly recommended to use abstract class?</p>
  
  <pre><code>//abstract
  class AbstractClass
  {
      // Force Extending class to define this method
      //abstract protected function getValue();
      //abstract protected function prefixValue($prefix);
  
      // Common method
      public function printOut() {
          print $this-&gt;getValue() . "\n";
      }
  }
  
  class ConcreteClass1 extends AbstractClass
  {
      protected function getValue() {
          return "ConcreteClass1";
      }
  
      public function prefixValue($prefix) {
          return "{$prefix}ConcreteClass1";
      }
  }
  
  class ConcreteClass2 extends AbstractClass
  {
      public function getValue() {
          return "ConcreteClass2";
      }
  
      public function prefixValue($prefix) {
          return "{$prefix}ConcreteClass2";
      }
  }
  
  $class1 = new ConcreteClass1;
  $class1-&gt;printOut();
  echo $class1-&gt;prefixValue('FOO_') ."\n";
  
  $class2 = new ConcreteClass2;
  $class2-&gt;printOut();
  echo $class2-&gt;prefixValue('FOO_') ."\n";
  </code></pre>