Created
March 4, 2016 10:30
-
-
Save im4aLL/e1baaf6ed3614aa53af1 to your computer and use it in GitHub Desktop.
PHP OOP Abstract Interface Inheritance Factory Pattern Magic method
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 | |
/** | |
* common things about a developer | |
*/ | |
abstract class Developer { | |
protected $_languages; | |
protected $_characteristics; | |
abstract public function setLanguages($language); | |
abstract public function getLanguages(); | |
abstract public function setCharacteristics($characteristics); | |
abstract public function getCharacteristics(); | |
abstract public function display(); | |
} | |
/** | |
* Extra things that a developer / others might want to do | |
*/ | |
interface Others { | |
public function doTravel(); | |
} | |
/** | |
* Web developer | |
*/ | |
class WebDeveloper extends Developer implements Others { | |
public $doTravel = FALSE; | |
protected $_activities = []; | |
protected $_name = 'Anonymous'; | |
public function __construct($name = NULL) { | |
if($name) $this->_name = $name; | |
} | |
public function __toString() { | |
return $this->display(); | |
} | |
public static function set($name) { | |
return new self($name); | |
} | |
public function setLanguages($language = []) { | |
$this->_languages = $language; | |
return $this; | |
} | |
public function getLanguages() { | |
return $this->_languages; | |
} | |
public function setCharacteristics($characteristics = []) { | |
$this->_characteristics = $characteristics; | |
return $this; | |
} | |
public function getCharacteristics() { | |
return $this->_characteristics; | |
} | |
public function setActivities($activity = []) { | |
$this->_activities = $activity; | |
return $this; | |
} | |
public function getActivities($activity = []) { | |
return $this->_activities; | |
} | |
public function display() { | |
return json_encode([ | |
'name' => $this->_name, | |
'know' => $this->getLanguages(), | |
'is' => $this->getCharacteristics(), | |
'activities' => $this->getActivities(), | |
]); | |
} | |
public function doTravel() { | |
return $this->doTravel; | |
} | |
} | |
$me = WebDeveloper::set('Habib Hadi'); | |
$me->setLanguages(['JavaScript', 'PHP']) | |
->setCharacteristics(['Self constructed', 'Creative', 'Curious']) | |
->setActivities(['Watch Game of Thrones', 'Like Heisenberg', 'Play Sniper Ghost Warrior']); | |
echo $me; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment