Pada konsep Object-Oriented-Programing,
terdapat banyak fungsi yang dapat mempermudah dalam membuat sebuah
aplikasi website. Salah dua dari beberapa fungsi tersebut adalah fungsi
__constructor() dan fungsi __destructor().
Saat object di inisialisasi,
kadang-kadang terbesit niat untuk mensetting beberapa hal terlebih
dahulu. Untuk menangani hal ini, PHP menyediakan fungsi ajaib ( the magic method) __construct(), yang
akan dipanggil secara otomatis kapanpun object dibuat. Untuk
mengilustrasikan konsep konstruktor ini, tambahkan konstruktor ke dalam
MyClass, yang akan menampilkan pesan kapanpun objek baru dari kelas
dibentuk:
<?php |
class
MyClass {
public
$prop1
= "I'm a class property!";
public
function
__construct()
{
echo
'The class "', __CLASS__, '" was initiated!<br />';
}
public
function
setProperty($newval)
{
$this->prop1 = $newval;
}
public
function
getProperty()
{
return
$this->prop1 . "<br />";
}
}
// Create a new object
$obj
= new
MyClass;
// Get the value of $prop1
echo
$obj->getProperty();
// Output a message at the end of the file
echo
"End of file.<br />";
?>
Catatan: __CLASS__
adalah sebuah magic constant, yang mana, dalam kasus ini, menghasilkan
nama kelas yang dipanggil. Ada beberapa magic constant lain yang
disediakan PHP, yang dapat dibaca di manual PHP di http://us3.php.net/manual/en/language.constants.predefined.php.
Refresh browser, dan hasil eksekusi scriptnya adalah:
The class "MyClass" was initiated! |
I'm a class property!
End of file.
Untuk memanggil fungsi saat objek dibersihkan, fungsi __destruct()
bisa kita gunakan. Magic constant ini berguna untuk membersihkan kelas
(sebagai contoh koneksi database). Menampilkan pesan pada browser saat
objek dibersihkan menggunakan fungsi __destruct() pada MyClass:
<?php |
class
MyClass {
public
$prop1
= "I'm a class property!";
public
function
__construct()
{
echo
'The class "', __CLASS__, '" was initiated!<br />';
}
public
function
__destruct()
{
echo
'The class "', __CLASS__, '" was destroyed.<br />';
}
public
function
setProperty($newval)
{
$this->prop1 = $newval;
}
public
function
getProperty()
{
return
$this->prop1 . "<br />";
}
}
// Create a new object
$obj
= new
MyClass;
// Get the value of $prop1
echo
$obj->getProperty();
// Output a message at the end of the file
echo
"End of file.<br />";
?>
Ketika skrip ditambah dengna listing destructor, hasil dari eksekusi program adalah sebagai berikut:
The class "MyClass" was initiated! |
I'm a class property!
End of file.
The class "MyClass"
was destroyed.
Sebenarnya, saat sebuah program PHP
dieksekusi, PHP secara otomatis akan melepaskan (membebaskan) semua
resource atau sumber daya yang dipakai, semisal memori. Hal ini
bertujuan untuk memastikan ketersediaan memori bagi program-program
lain. Proses inilah yang berperan sebagai pelatuk untuk menjalankan
destructor objek MyClass. .
Untuk memicu destructor secara ekspilsit, kita bisa menghancurkan sebuah objek menggunakan fungsi unset():
Untuk memicu destructor secara ekspilsit, kita bisa menghancurkan sebuah objek menggunakan fungsi unset():
<?php |
class
MyClass {
public
$prop1
= "I'm a class property!";
public
function
__construct()
{
echo
'The class "', __CLASS__, '" was initiated!<br />';
}
public
function
__destruct()
{
echo
'The class "', __CLASS__, '" was destroyed.<br />';
}
public
function
setProperty($newval)
{
$this->prop1 = $newval;
}
public
function
getProperty()
{
return
$this->prop1 . "<br />";
}
}
// Create a new object
$obj
= new
MyClass;
// Get the value of $prop1
echo
$obj->getProperty();
// Destroy the object
unset($obj);
// Output a message at the end of the file
echo
"End of file.<br />";
?>
Sekarang, hasil dari eksekusi program akan terlihat sama seperti memakai fungsi __destructor(). Jalankan program, dan seharusnya di browser tampil seperti ini:
The class "MyClass" was initiated! |
I'm a class property!
The class "MyClass"
was destroyed.
End of file
Demikianlah artikel tentang fungsi destructor dan constructor pada Object-Oriented-Programing menggunakan PHP. Semoga bermanfaat
link sobat sudah sy pasang juga...
BalasHapushttp://azzam10.blogspot.com
Wah sebenernya pengen juga jadi programer gan tp apa daya heee...
BalasHapus