Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save msankhala/7a27b2ab006d2998fd54432b88cf35fc to your computer and use it in GitHub Desktop.
Save msankhala/7a27b2ab006d2998fd54432b88cf35fc to your computer and use it in GitHub Desktop.
A simple gist to show PHP Abstract class and Late Static Binding and Magic method.
<?php
// Problem
/**
* Review the code:
* 1) Find errors
* 2) What would be improved from the design point of view?
* How to get rid of implementing not used methods in the child classes?
* 3) Implement a counter for the created instances of any class from the hierarchy.
* 4) Implement getCategory method so it returns the value of the constant
* defined in the child class.
*/
class ParentEntity
{
abstract public function getName(): string;
abstract public function getTitle(): string;
abstract public function getDescription(): string;
protected function getCategory(): string
{
// TBD
}
}
class FirstChildEntity extends ParentEntity
{
public const ENTITY_CATEGORY = "first";
public function getName(): string
{
return "Child 1";
}
public function getTitle(): string
{
return "Title 1";
}
public function getDescription(): string
{
throw new NotImplementedException("getDescription function is not be implemented under the first child");
}
}
class SecondChildEntity extends ParentEntity
{
public const ENTITY_CATEGORY = "second";
public function getName(): string
{
throw new NotImplementedException("getName function is not be implemented under the first child");
}
public function getTitle(): string
{
throw new NotImplementedException("getTitle function is not be implemented under the first child");
}
public function getDescription(): string
{
return "Description 2";
}
}
class ThirdChildEntity extends ParentEntity
{
public const ENTITY_CATEGORY = "third";
public function getName(): string
{
return "Name 3";
}
public function getTitle(): string
{
return "Title 3";
}
public function getDescription(): string
{
return "Description 3";
}
}
// Solution
abstract class ParentEntity
{
static $count = 0;
public function __construct() {
static::$count++;
}
abstract public function getName(): string;
abstract public function getTitle(): string;
abstract public function getDescription(): string;
protected function getCategory(): string
{
return static::ENTITY_CATEGORY;
}
}
abstract class FirstChildEntity extends ParentEntity
{
public const ENTITY_CATEGORY = "first";
static $count = 0;
public function getName(): string
{
return "Child 1";
}
public function getTitle(): string
{
return "Title 1";
}
abstract public function getDescription(): string;
public function getCategory(): string
{
return parent::getCategory();
}
}
class SecondChildEntity extends FirstChildEntity
{
public const ENTITY_CATEGORY = "second";
static $count = 0;
public function getDescription(): string
{
return "Description 2";
}
public function getCategory(): string
{
return parent::getCategory();
}
public function getCounter() {
echo static::$count;
}
}
class ThirdChildEntity extends SecondChildEntity
{
public const ENTITY_CATEGORY = "third";
static $count = 0;
public function getName(): string
{
return "Name 3";
}
public function getTitle(): string
{
return "Title 3";
}
public function getDescription(): string
{
return "Description 3";
}
public function getCategory(): string
{
return parent::getCategory();
}
public function getCounter() {
echo static::$count;
}
public function __clone() {
static::$count++;
}
}
$third = new ThirdChildEntity();
echo $third->getCategory() . PHP_EOL;
$second = new SecondChildEntity();
echo $second->getCategory() . PHP_EOL;
// $first = new FirstChildEntity();
// echo $first->getCategory();
$obj1 = new ThirdChildEntity();
$obj2 = new ThirdChildEntity();
$obj2->getCounter() . PHP_EOL;
$obj3 = clone $obj2;
$second->getCounter() . PHP_EOL;
$obj3->getCounter() . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment