19 November 2014

Magic Method di PHP

Apa itu magic method ?
 Ketika membuat kelas di PHP, ada beberapa nama method yang tidak dapat digunakan, kecuali memang dimaksudkan untuk menggunakannya dengan cara yang telah dimaksudkan oleh PHP itu sendiri, dan setiap nama magic method diawali dengan dua underscore, seperti __contruct,  __destruct(),  __set(),  __get(),  __call(),  __callStatic(),  __isset(),  __unset(),  __sleep(),  __wakeup(),  __toString(),  __invoke(),  __set_state(),  __clone()



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154


class Item{
 protected $id = null;
 protected $name = null;
 protected $price = 0;
 protected $qty = 0;
 private $totalPrice = 0;
 
 // Adalah method yang dipanggil di saat kelas diinisiasi menjadi objek. 
 function __construct(){
  if(null === $this->id){
   $this->id = mt_rand();
   echo "__construct ? >> ";
  }  
 }
 
 // Adalah method yang dipanggil ketika properti kelas menerima nilai. 
 function __set($name, $value){
  $this->$name = $value;
  if($name === "qty" || $name === "price"){
   $this->setTotalPrice();
  } 
  echo "__set >> Set variabel : $name dengan nilai : $value ";
 }
 
 // Adalah method yang dipanggil ketika properti kelas dipanggil. 
 function __get($name){
  echo "__get >> Ambil variabel : $name nilai dengan : " . $this->{$name} . "";
  return $this->$name;
 }
 
 // Magic method yang dipanggil ketika objek dihancurkan 
 function __destruct(){
  echo "__destruct >>  Item ID : " . $this->id ."";
 } 
 
 // Magic method yang dipanggil ketika method yang tidak bisa diakses / tidak ada pada kelas dipanggil. 
 function __call($name, $arguments){
  echo "Fungsi  : " . $name;
  if (!empty($arguments)):
   echo "- Argumen : " ;
   print_r($arguments);
  endif;
 }
 
 // Magic method yang dipanggil ketika method static yang tidak bisa diakses / tidak ada pada kelas dipanggil. 
 static function __callStatic($name, $arguments){
  echo "Fungsi  : " . $name;
  if (!empty($arguments)):
   echo "- Argumen : " ;
   print_r($arguments);
  endif;
 }
 
 // Dipanggil ketika objek di serialize 
 function __sleep(){
  return array("name", "price");
 }
 
 // Dipanggil ketika objek di unserialize
 function __wakeup(){
  echo "__wakeup";
 }
 
 // Dipanggil ketika objek dijalankan seperti fungsi.
 function __invoke(){
  echo "__invoke >> INVOKE";
 }
 
 // Dipanggil ketika objek di klon ke variabel lain,
 function __clone(){
  echo "__clone >> Objek di klon.";
 }
 
 // DIpanggil ketika kelas di export menggunakan fungsi var_export()
 static function __set_state(){
  echo "__set_state >>";
 }
 
 // Magic method yang dipanggil ketika fungsi empty() , isset() pada properti yang tidak bisa diakses dipanggil
 function __isset($var){
  echo "Isset: Variabel $var ini tidak ada.";
 }
 
 // Magic method yang dipanggil ketika fungsi unset dipanggil pada properti yang tidak bisa diakses dipanggil
 function __unset($var){
  echo "Unset: Variabel $var ini tidak ada.";
 } 
 
 // Magic method yang dipanggil ketika objek diperlakukan seperti String
 function __toString(){
  return "Class : " . json_encode(__CLASS__);
 } 
 
 // bukan magic method  
 function setTotalPrice(){
  $this->totalPrice = $this->price * $this->qty;
 }
 
 // bukan magic method  
 function getTotalPrice(){
  return $this->totalPrice;
 }
 
 // bukan magic method
 private function diskon(){
 
 }
 
 function detail(){
  print_r($this);
 }
}

// __construct
$item = new Item;
echo "<br>";
// __set
$item->name = "Keripik Singkong Pedas";
$item->qty = 10;
$item->price = "1500";
// __get
echo $item->totalPrice . '<br>';
// __call
// method diskon tidak bisa diakses karena private
$item->diskon("id item", array("harga"=>1000, "diskon"=>100));
echo '<br>';
// __callStatic
$item->tesStatic("Fungsi Statik", "Argumen");
echo '<br>';
// __isset
isset($item->id);
echo '<br>';
// __unset
unset($item->id);
echo '<br>';
//__sleep
$itemS = serialize($item);
// __toString
echo $itemS.'<br>';
// __wakeup
echo unserialize($itemS);
echo '<br>';
// __invoke
$item(90);
echo '<br>';
// __clone
$clone = clone($item);
echo '<br>';
var_export($clone);
echo '<br>';

0 komentar:

Posting Komentar