Skip to content

Instantly share code, notes, and snippets.

@anovsiradj
Created May 28, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anovsiradj/e7f32a95a783e11dd47054be4119549e to your computer and use it in GitHub Desktop.
Save anovsiradj/e7f32a95a783e11dd47054be4119549e to your computer and use it in GitHub Desktop.
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