OOP PHP – Constant Variable

<?php
/*
constant dapat diturunkan pada subclass turunan nya
*/

class NilaiPhi{
    const phi = 3.14;
}

class BaruPhi extends NilaiPhi{

}

echo "NilaiPhi::phi = ".NilaiPhi::phi;

echo "<br />BaruPhi::phi = ".BaruPhi::phi;

/*
NilaiPhi::phi = 3.14
BaruPhi::phi = 3.14
*/
?> 


<?php 
/* 
constant digunakan untuk deklarasi nilai yg tidak boleh diubah-ubah 
*/ 
 
class NilaiPhi{ 
    const phi = 3.14; 
} 
$x = new NilaiPhi(); 
 
#nilai ini yg ditampilkan 
echo "method 1 = ".NilaiPhi::phi; 
 
#nilai ini tidak ditampilkan 
echo "<br />method 2 = ".$x->phi; 
 
/* 
method 1 = 3.14 
method 2 =  
*/ 
?>

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