Class Object dengan PHP
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 | |
// 201602211052, 201605282339 | |
error_reporting(E_ALL); ini_set('display_errors', 1); | |
class Anoop { | |
public function bend($elm,$attack) { | |
if (!isset($this->{$elm})) { | |
$this->{$elm} = new ELement($elm); | |
} | |
$this->{$elm}->technique($attack); | |
} | |
} | |
class ELement { | |
private $jurus = []; | |
private $index; | |
public function technique($jurus) { | |
$this->jurus[] = $jurus; | |
$this->index = count($this->jurus)-1; | |
} | |
public function attack($index = null) { | |
$this->index = $index === null ? $this->index : $index; | |
$jurus = $this->jurus[$this->index]; | |
echo " -----> ".$jurus."\n"; | |
} | |
} | |
echo "<pre>"; | |
$anoop = new Anoop; | |
$anoop->bend('water','Shark Bite'); | |
$anoop->water->attack(); | |
$anoop->bend('water','Tsunami Wall'); | |
$anoop->water->attack(); | |
$anoop->bend('wind','Tornado Needle'); | |
$anoop->wind->attack(); | |
$anoop->bend('fire','Merapi Wedhus Gembel'); | |
$anoop->fire->attack(); | |
$anoop->water->attack(0); | |
/* | |
result: | |
--------------------------- | |
-----> Shark Bite | |
-----> Tsunami Wall | |
-----> Tornado Needle | |
-----> Merapi Wedhus Gembel | |
-----> Shark Bite | |
--------------------------- | |
*/ |
Komentar
Posting Komentar
Komentar ...