Postingan lainnya
setter dan getter php
<?php
class Coba {
public $value;
public function setVal($value){
$this->$value = $value;
}
public function getVal(){
return $this->value;
}
}
$my = new Coba;
$my->setVal('coba Nilai');
echo $my->getVal();
saat saya coba jalankan di browser tidak nampilin nilai/blank saya ingin outputnya string "coba Nilai" masalahnya dimana ya gan ?
0
3 Jawaban:
Jawaban Terpilih
coba seperti ini
<pre> // fungsi setter public function setVal($value){ $this->value = $value; //pada bagian setelah this di hilangkan $ } </pre>
<pre> //fungsi getter public function getVal(){ return $this->value; } </pre>
<pre> $my = new Coba(); $my->setVal('coba Nilai'); echo $my->getVal(); </pre>
1
waduh,, iya gan saya typo ke isi lambang dollar
0