Friday, May 14, 2010

OOP In PHP

PHP supports OOP starting version 5, which had a new form object modeling to overcome the complexity problem that can not be done on the previous version. PHP 5 provides better performance and new features compared to previous versions.

1. Class
Each pendefinisain / declaration of a class in PHP using a class that followed the class name, then followed the '{' and ends with the sign '}' we can give any name of a class, while not the same as PHP functions owned. example, print class. print class name can not we use it, because the print is owned by a PHP function. We can load / define the property class and the method of the class among the "braces". variable $ this is a fictitious variable that is used to summon the properties or method of a class. The following examples will further clarify about the class in php
//kelas orang
class orang{
//Properties Class
var $nama;
var $umur;
var $tinggi;
var $berat;

function orang (){//Constructor
echo "Initialisation Object";
}

function get_nama(){
return $this->nama;
}
}//end class




in the example above I created a class with people's names. These classes have the properties name, age, height and weight. Function is a constructor of one class of people, contstructor will be executed when we create a person object.

2. Property Class
Property class is a class of identity / special character of a class, an example would memilki The height and weight. Height and weight were sebuat property. in the example above the class has a property name, age, height and weight. To give the value of the property to a class we can not use a direct way, like assign a value to a varible. For example, suppose that we would put a value on property variable name with value "Rakhmad maulidi", to give value to the property class we use the variable $ this, To access / menggambil value property of a class also use the variable $ this example will further clarify how give value-class properties in PHP

$nama="Rakhmad Maulidi";
$this->nama="Rakhmad Idiluam";

echo "$nama";//result : Rakhmad Maulidi
echo $this->nama;//result : Rakhmad Idiluam

cara di atas bisa kita jadikan dalam bentuk function berikut :

function set_nama($nama){
$this->nama=$nama;
}

function get_nama(){
return $this->nama;
}


3. Method / Class Behavior
Method / Class Behavior is what functions could be performed by an object. Suppose that in the class of people can do to eat, sleep, walk. The following examples will further clarify on how to provide the method / class behavior in PHP
function makan($is_makan)
{
if ($is_makan){
$mkn = "sedang makan";}
else{
$mkn ="sudah makan";
}
return $mkn;
}

function tidur($is_tidur)
{
if ($is_tidur){
$tdr= "sedang tidur";}
else{
$tdr="sudah bangun";
}
return $tdr;
}


Now, after the intermezzo which I gave at the top, now is the time coding a more concrete about OOP in PHP. To create an object from a class we use a new notation, eg
$rahmat = new orang;
$andi = new orang;
$ali = new orang;


In this example I made three people object to the $ Rahmad, $ andi and $ ali. Each object will have its own property, I suppose that would membeikan value "Rakhmad Idiluam" of the property name of the object $ Rahmad, and on other objects:
$rahmad->set_nama("Rakhmad Maulidi");
$andi->set_nama("Andi Malarangeng");
$ali ->set_nama("Ali bin Abu Tholib");


To get the value of each property name of the object we can perform the following ways:
$rahmad->get_nama();
$andi->get_nama();
$ali->get_nama();


to use the method on an object or behavior we can do the following:
$rahmad->tidur(true);
$andi->makan(true);
$ali->get_nama(false);

No comments:

Post a Comment