1

I have a base class called BaseRecurring.

It has a protected function called _checkCurrentMonth

Inside the _checkCurrentMonth,

My code inside the BaseRecurring class is

protected function _checkNextMonth($type, $startDate = 1, $endDate = 1)
{
    $incrementToFirstDay = $startDate - 1;
    $incrementToLastDay = $endDate - 1;

    $startDate = new \DateTime('first day of this month');
    $endDate = new \DateTime('first day of next month');

    if ($incrementToFirstDay > 0 || $incrementToLastDay > 0) {
        // e.g. if we want to start on the 23rd of the month
        // we get P22D
        $incrementToFirstDay = sprintf('P%dD', $incrementToFirstDay);
        $incrementToLastDay = sprintf('P%dD', $incrementToLastDay);

        $startDate->add(new \DateInterval($incrementToFirstDay));
        $endDate->add(new \DateInterval($incrementToLastDay));
    }

    $this->checkMonth($type, $startDate, $endDate);
}

The issue is that I don't want the base class to define the implementation for checkMonth. I want the child class to implement checkMonth method.

I intend to have an interface called CheckMonthInterface that will explicitly state a method called checkMonth.

So do I have the base class implement the CheckMonthInterface and then keep that method empty?

or do I have the base class NOT implement the CheckMonthInterface and then have the child class implement it?

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282

1 Answers1

1

It all depends on the logic you need, but usually there are 2 common ways:

  • define an abstract parent class (think about it like a generic line) and add an abstract method, so then non-abstract children will have to add their own implementation.
  • define an interface (think about it like a contract to implement something common) and add it to those classes that must have this implementation.

This link can be useful too: Abstract Class vs. Interface

Example:

<?php

abstract class Polygon
{
    protected $name;

    abstract public function getDefinition();

    public function getName() {
        return $this->name;
    }
}

class Square extends Polygon
{
    protected $name = 'Square';

    public function getDefinition() {
        return $this->getName() . ' is a regular quadrilateral, which means that it has four equal sides and four equal angles (90-degree angles, or right angles).';
    }
}

class Pentagon extends Polygon
{
    protected $name = 'Pentagon';
}

echo (new Square())->getDefinition(); // Square is a regular quadrilateral, which means that it has four equal sides and four equal angles (90-degree angles, or right angles).
echo (new Pentagon())->getDefinition(); // PHP Fatal error: "class Pentagon contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Polygon::getDefinition)"
Community
  • 1
  • 1
Axalix
  • 2,831
  • 1
  • 20
  • 37
  • If you noticed, I actually have a fully implemented function inside the base class. I don't think abstract class allows that. Or am I wrong? – Kim Stacks Mar 06 '16 at 02:28
  • Abstract class cannot be instantiated (PHP Fatal error: `Cannot instantiate abstract class`), but can (usually should) have methods, which implementation will be available for children. – Axalix Mar 06 '16 at 02:40