OOP PHP – Object Cloning

pemberian nilai variabel pada object adalah sebagai reference, artinya bila dilakukan operasi yang mengubah nilai variabel, nilai variabel asli juga berubah

<?php

include "rekening.php";
$rekRian = new RekeningTabungan();
$rekRian->setor(25000);
$rekIsal = $rekRian;
$rekIsah = clone $rekRian;

$rekRian->setor(15000);

$saldo_rian = $rekRian->cekSaldo();
$saldo_isal = $rekIsal->cekSaldo();
$saldo_isah = $rekIsah->cekSaldo();

echo "Rian = Rp. " . $saldo_rian."<br />";
echo "Isal = Rp. " . $saldo_isal."<br />";
echo "Isah = Rp. " . $saldo_isah."<br />";

?>

Rian = Rp. 80000
Isal = Rp. 80000
Isah = Rp. 65000

perbedaan nilai rekening diakbibatkan dari nilai isah berdasarkan value, dan isal berdasarkan reference

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s