//
you're reading...
IT-Center, PHP, Web Based

PHP 5 OOP ( php class)

A. CLASS

<?php
class Item {
protected $name, $price, $qty, $total;

public function __construct($iName, $iPrice, $iQty) {
$this->name = $iName;
$this->price = $iPrice;
$this->qty = $iQty;
$this->calculate();
}

protected function calculate() {
$this->price = number_format($this->price, 2);
$this->total = number_format(($this->price * $this->qty), 2);
}

public function __toString() {
return "You ordered ($this->qty) '$this->name'" . ($this->qty == 1 ? "" : "s") .
" at \$$this->price, for a total of: \$$this->total.";
}
}

echo (new Item("Widget 22", 4.90, 2));
?>
You ordered (2) 'Widget 22's at $4.90, for a total of: $9.80.

By loading class Item (which houses all the improvements we made over the first script) into PHP first, we went from having to write 5 statements in the first script, to writing only 1 statement "echo new Item" in the second.

 

B. CONSTRUCT & DESTRUCT

Sometimes you just forget to close handles, links, etc and sometimes you are just lazy to do that. PHP 5 OOP can do it automatically by using destructors:

<?php
class MySqlDriver {
private $_Link;

public function __construct( <...> ) {
$this->_Link = mysql_connect( <...> );
}

// this will be called automatically at the end of scope
public function __destruct() {
mysql_close( $this->_Link );
}
}

$_gLink = new MySqlDriver( <...> );
// and you don't need to close the link manually
?>

C. INHERITED

You can call a function defined in an inherited class from the parent class. This works in both PHP 4.3.6 and 5.0.0:

<?php

class p {

function p() {
print "Parent's constructor\n";
}

function p_test() {
print "p_test()\n";
$this->c_test();
}
}

class c extends p {

function c() {
print "Child's constructor\n";
parent::p();
}

function c_test() {
print "c_test()\n";
}
}

$obj = new c;
$obj->p_test();

?>

Outputs:

Child's constructor
Parent's constructor
p_test()
c_test()

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.

Discussion

No comments yet.

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

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

December 2011
M T W T F S S
« Nov   Jan »
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Web Statistic

Blog Stats

  • 149,580 hits
%d bloggers like this: