Latihan class dan function dalam PHP contoh kasus jual beli - Pemrograman Web Dinamis
Program sederhana penggunaan class dan function dalam PHP dengan contoh kasus jual beli
jualbeli.php
<?php
class Jualbeli{
var $tskode;
private $jenis = "Alat Tulis Kantor";
private $tsnamabarang = "Pulpen";
protected $jmlbeli = 4;
protected $tsharga = 2000;
function barang(){
return "Nama barang = $this->tsnamabarang";
}
function jenis(){
return "Jenis barang = $this->jenis";
}
}
class Pembelian extends Jualbeli{
function harga(){
return "Harga barang = $this->tsharga";
}
function jmlbeli(){
return "Jumlah beli = $this->jmlbeli";
}
function total(){
return ($this->tsharga)*($this->jmlbeli);
}
}
$jb = new Jualbeli();
$jb->tskode = "1100101010";
$jb2 = new Pembelian();
echo "Kode Barang = $jb->tskode <br>";
echo $jb->jenis()."<br>";
echo $jb->barang()."<br>";
echo $jb2->harga()."<br>";
echo $jb2->jmlbeli()."<br>";
echo "Total Harga = ".$jb2->total()."<br>";
$jb3 = new Pembelian();
echo "Diskon pembelian = ".(20/100)*$jb3->total();
?>
Hasil run :
jualbeli.php
<?php
class Jualbeli{
var $tskode;
private $jenis = "Alat Tulis Kantor";
private $tsnamabarang = "Pulpen";
protected $jmlbeli = 4;
protected $tsharga = 2000;
function barang(){
return "Nama barang = $this->tsnamabarang";
}
function jenis(){
return "Jenis barang = $this->jenis";
}
}
class Pembelian extends Jualbeli{
function harga(){
return "Harga barang = $this->tsharga";
}
function jmlbeli(){
return "Jumlah beli = $this->jmlbeli";
}
function total(){
return ($this->tsharga)*($this->jmlbeli);
}
}
$jb = new Jualbeli();
$jb->tskode = "1100101010";
$jb2 = new Pembelian();
echo "Kode Barang = $jb->tskode <br>";
echo $jb->jenis()."<br>";
echo $jb->barang()."<br>";
echo $jb2->harga()."<br>";
echo $jb2->jmlbeli()."<br>";
echo "Total Harga = ".$jb2->total()."<br>";
$jb3 = new Pembelian();
echo "Diskon pembelian = ".(20/100)*$jb3->total();
?>
Hasil run :
Komentar
Posting Komentar