Friday, May 4, 2007

PHP Class

I found a great thing in PHP 5, which is the real class concept has already
adopted ( similar to other language like delphi). So that, OOP (Object
Oriented Programming) can be implemented very well.

In general, PHP class has structure like this

class aname() {
//definition of variable, in delphi called property
var ....

//constructor, in delphi would be Create
function __constructor(){
//initial value should be placed here
......
}
function a(){
...
}
function b(){
....
}
}


Ideally, a class should be has Get and Set method
Get is to retrieve data from a class
Set is to place data into a class

example:

class person() {
var $IdentityNo,
$Name;

function __construct(){
$this->$IdentityNo="";
$this->$Name="";
}

function GetIdentityNo(){
return $this->$IdentityNo;
}
function GetName(){
return $this->$Name;
}

function SetIdentityNo($AIdentityNo){
$this->$IdentityNo=$AIdentityNo;
}
function SetIdentityNo($AName){
$this->$Name=$AName;
}
}

usage:

$p=new person; //construc
$p->SetIdentityNo("001"); //Set Identity No
$p->SetName("Paijo"); //Set Name

echo $p->GetIdentityNo(); //print Identity No
echo $p->GetName(); //print Name


that's a simple, right :)

No comments: