Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
playing with php class object
<?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
---------------------------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment