Introduction to PHP - Abstract Methods Part 1

Guest Posts Paolo Nikko Nuñal

OOP can be quite frustrating most especially if you have little or know knowledge about it. Well in this tutorial we are going to learn abstract classes and how you can implement it to your program. Most of the time you will ask yourself what are abstract classes. To put it into perspective, abstract classes are pretty much the same as normal classes but consider it as classes that are partially implemented by a programmer.

Abstract classes may contain one or more abstract methods. Abstract methods is simply a function definition that serves as a 'guide' that tells the programmer extending to the abstract class that it must implement it to the child class.

abstract class Stats{
  private $intelligence;
  private $strength;
  private $agility;

  abstract function statSheet();
  abstract function setStats($intelligence,$strength,$agility);

class Hero extends Stats{
  public function statSheet(){
  public function setStats($intelligence,$strength,$agility){

} ?>

From the code above we have created an abstract class called Stats in which it contains 3 private fields and 1 abstract function. Afterwhich we have created a Hero class that extends to the abstract class called stats. When creating the Hero class, we used the statSheet() method found on its parent class as well as the setStats. We need to implement these methods since these are abstract methods.