-
-
Save tommcfarlin/5b2936875807f041e98a87f8a5627e8b to your computer and use it in GitHub Desktop.
[OOP Abstract Classes] Abstract Classes, Part 1 - Abstracting Behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Taxonomy | |
{ | |
private $taxonomyName; | |
private $taxonomySlug; | |
public function __construct($name) { | |
$this->taxonomyName = $name; | |
$this->taxonomySlug = strtolower(str_ireplace(' ', '-', $this->taxonomyName)); | |
} | |
public function getName() { | |
return $this->taxonomyName; | |
} | |
public function getSlug() { | |
return $this->taxonomySlug; | |
} | |
abstract protected function isHierarchical(); | |
abstract protected function isCategory(); | |
abstract protected function isTag(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Tag extends Taxonomy | |
{ | |
protected function isHierarchical() { | |
return false; | |
} | |
protected function isCategory() { | |
return $this->isHierarchical; | |
} | |
protected function isTag() { | |
return !$this->isHierarchical; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Category extends Taxonomy | |
{ | |
private $parentId = -1; | |
protected function isHierarchical() { | |
return true; | |
} | |
protected function isCategory() { | |
return $this->isHierarchical; | |
} | |
protected function isTag() { | |
return !$this->isHierarchical; | |
} | |
public function setParentId($parentId) { | |
$this->parentId = $parentId; | |
} | |
public function getParentId() { | |
return $this->parentId; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Taxonomy | |
{ | |
private $taxonomyName; | |
private $taxonomySlug; | |
public function __construct($name) { | |
$this->taxonomyName = $name; | |
$this->taxonomySlug = strtolower(str_ireplace(' ', '-', $this->taxonomyName)); | |
} | |
public function getName() { | |
return $this->taxonomyName; | |
} | |
public function getSlug() { | |
return $this->taxonomySlug; | |
} | |
abstract protected function isHierarchical(); | |
abstract protected function isCategory(); | |
abstract protected function isTag(); | |
} | |
/*--*/ | |
class Tag extends Taxonomy | |
{ | |
protected function isHierarchical() { | |
return false; | |
} | |
protected function isCategory() { | |
return $this->isHierarchical; | |
} | |
protected function isTag() { | |
return !$this->isHierarchical; | |
} | |
} | |
/*--*/ | |
class Category extends Taxonomy | |
{ | |
private $parentId = -1; | |
protected function isHierarchical() { | |
return true; | |
} | |
protected function isCategory() { | |
return $this->isHierarchical; | |
} | |
protected function isTag() { | |
return !$this->isHierarchical; | |
} | |
public function setParentId($parentId) { | |
$this->parentId = $parentId; | |
} | |
public function getParentId() { | |
return $this->parentId; | |
} | |
} | |
/*- Tag Demo ----------------------------*/ | |
$tag = new Tag('Acme Tag'); | |
echo $tag->getName(); | |
echo $tag->getSlug(); | |
/*- Category Demo -----------------------*/ | |
$category = new Category('Acme Category'); | |
echo $category->getName(); | |
echo $category->getSlug(); | |
echo $category->getParentId(); | |
$category->setParentId(100); | |
echo $category->getparentId(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment