Creating a Class with Properties and Methods

Creating a Class with Properties and Methods on PHP programming

example (contoh):

A class can be created using code similar to the following:

class Foo
{
var $foo;
var $bar;

function setBar ($bar)
{
$this->bar = $bar;
}
function getBar ()
{
return $this->bar;
}
}

This class has the following characteristics:

* The name of the class is 'Foo'.
* It contains the variables (properties) '$foo' and '$bar'.
* It contains functions (methods) called 'setBar' and 'getBar'.
* Function 'setBar' is used to insert data into the object variable '$bar'.
* Function 'getBar' is used to retrieve data from the object variable '$bar'.

In theory you are supposed to have a 'set' method and a 'get' method for each variable within the class, one to put data in and the other to get data out. These are commonly referred to as 'setters' and 'getters'.

Note here that the syntax $this->bar is used to reference an object variable from within that object. These variables can be referenced by any function/method within the class. The syntax $bar identifies a variable whose scope is limited to the current function only.
The 'constructor' method

A class can contain a special method known as a 'constructor' which is automatically processed whenever an instance of that class is created. In PHP 4 this is a method which has the same name as the class. This can be used to set initial data for the object, as shown in the following example:

class Foo
{
var $foo;
var $bar;

function Foo ()
{
$this->foo = 'initial value for $foo';
$this->bar = 'initial value for $bar';
}
}

In PHP 5 you can use the standard name __construct() as the constructor.
Extending a Class

It is possible to create a new class which 'extends' an existing class. By this I mean that you can inherit all the properties and methods of the existing class, and either provide alternative code for existing methods or add completely new methods. An example of how to do this is shown below:

require_once 'foo.class.inc';
class Bar extends Foo
{
var $tom;
var $dick;
var $harry;

function setTom ($tom)
{
$this->tom = $tom;
}
function getTom ()
{
return $this->tom;
}
}

Note that you have to include the definition of the parent class before you can extend it.

Class Bar is now an extension of class Foo. It has the following characteristics:

* It has all the properties and methods of class Foo plus some properties and methods of its own.
* If class Bar contained a method with the same name as a method within class Foo then the Bar method would replace the Foo method.
* If class Bar does not have a constructor of its own then the constructor in class Foo will be used instead.

Creating an Object

Now that we have created a class how do we use it? The first step is to create an instance of the class known as an object. Note that you must include the definition of the class before you can create an object from that class, as shown below:

include 'foo.class.inc';
$object = new Foo;

Here the object is called '$object', but I could have used any name. Note that it is possible to create more than one object from the same class:

include 'foo.class.inc';
$tom = new Foo;
$dick = new Foo;
$harry = new Foo;

Accessing an Object's Properties and Methods

In order to perform a method within an object you need to specify both the object name and the function name as in:

$result = $tom->setFoo('value');

It is also possible to access an object's properties directly without going through a method, as in:

$var = $tom->Foo;
$tom->Foo = $var;

Although this approach is perfectly valid I should point out that if at some time in the future you decide that it is necessary to do some extra processing on the data before it is moved in or out of your object then you will have to modify all those places where the data is referenced. On the other hand if you force all object properties to be accessed though a get or set method then you will only have to change the contents of that method just the once.

You may have noticed that when you are outside of an object and you want to access the object's properties or methods you must specify the object's identifier as in $tom-> or $dick-> or $harry->, but when you are inside an object you can use the magic word $this-> as the object identifier.
Objects and Sessions

You may or may not be aware that you can maintain data between the execution of one script and another by using PHP's session capability. It is also possible to save an object's properties in this session data so that it can be reinstated by the next script within the same session. You can save an object's properties by using the serialize() command as follows:

include 'foo.class.inc';
$dbobject = new Foo;
...
...
$_SESSION['dbobject'] = serialize($dbobject);

In a subsequent script you can reinstate the object to exactly the same condition by using the unserialize() command like this:

include 'foo.class.inc';
if (isset($_SESSION['dbobject'])) {
$dbobject = unserialize($_SESSION['dbobject']);
} else {
$dbobject = new Foo;
} // if

Comments

Galuh Parantri said…
bagus jugah nih ngasi referensi buat kita2 soal coding

tapi kok baru dikit ya postingnya...
banyakin dunk mas..mbak...
manidate said…
saya akan jelaskan cara nyari bilangan decimal ke bilangan binary
angka yg dicari adalah 12

decimal binary
12:2 0
6:2 0
3:2 1
1

pasti dibagi 2
kalo abis hasilnya 0
klo ga abis hasilnya 1
jadi hasilnya yg di atas adlah 1100
dari binary yang paling bawah diitungnya ke decimal yg paling atas.
yg decimal bkn yg terakhir ga diitung.
kalo yg terakhir sisa 1,ditulisnya di binary dan decimalnya 1 dan 1 dua-duanya.
karena klo misalnya 3 dibagi 2 hasil 1 en sisa 1.
sisa ditaro di binary, dan hasil di taro di decimal.

angka yg di cari adalh 11

decimal binary
11:2 1
5:2 1
2:2 0
1

kalo pas abis di decimal yg terakhir
tulis 0 di binary terakhir dan tulis 1 di decimal terakhir.
ngerti kan.............????

Popular posts from this blog

membuat antena untuk smart 1900MHZ

Mengetahui IP Address Pengirim Email