จาก ทฤษฏี อ่ะครับ เขาจะบอกให้เราสร้าง class ของแต่ละหัวข้อที่เราต้องการ เช่น
Code class ของการติดต่อฐานข้อมูล (PHP)
<?php
/**** Class Database ****/
Class MyDatabase
{
/**** function connect to database ****/
function MyDatabase($strHost,$strDB,$strUser,$strPassword)
{
$this->objConnect = mysql_connect($strHost,$strUser,$strPassword);
mysql_query("SET NAMES TIS620");
$this->DB = mysql_select_db($strDB);
}
/**** function select record ****/
function fncSelectRecordAll()
{
$strSQL = "SELECT * FROM $this->strTable ";
$objQuery = @mysql_query($strSQL);
return @mysql_fetch_array($objQuery);
}
/*** end class auto disconnect ***/
function __destruct() {
return @mysql_close($this->objConnect);
}
}
?>
ตอนนี้ class ที่เราสร้างขึ้นจะถูกเรียกว่า object แล้วครับ โดยจะมีชื่อตาม class ที่เราสร้าง เวลาที่เราเรียกใช้ ก้อ Code (PHP)
<?php
include("phpMySQLFunctionClassDatabase.php");
//**** New class database ****//
$strHost = "localhost";
$strDB = "myDB";
$strUser = "root";
$strPassword = "rootpassword";
$clsMyDB = new MyDatabase($strHost,$strDB,$strUser,$strPassword);
//**** Call to function select record ****//
$clsMyDB->strTable = "DataTable";
$objSelect = $clsMyDB->fncSelectRecordAll();
?>
class TemperatureDegrees{
private $value;
private $temperatureUnit;
public function __construct($value,$temperatureUnit){
$this->value=$value;
$this->temperatureUnit=$temperatureUnit;
$temperatureUnit->setTemperatureDegrees($this);
}
public function setTemperatureUnit($temperatureUnit){
$this->temperatureUnit=$temperatureUnit;
}
public function getValue(){
return $this->value;
}
public function setValue($value){
$this->value=$value;
}
public function __toString(){
return (string)$this->getValue();
}
public function add($addDegrees){
return $this->temperatureUnit->add($addDegrees);
}
public function addToSeft($addDegrees){
$this->temperatureUnit->addToSeft($addDegrees);
}
public function toCelsius(){
return $this->temperatureUnit->toCelsius();
}
public function toFahrenheit(){
return $this->temperatureUnit->toFahrenheit();
}
public function toKelvin(){
return $this->temperatureUnit->toKelvin();
}
}
class Celsius extends TemperatureDegrees{
public function __construct($value){
parent::__construct($value,new TemperatureUnitCelsius());
}
}
class Fahrenheit extends TemperatureDegrees{
public function __construct($value){
parent::__construct($value,new TemperatureUnitFahrenheit());
}
}
class Kelvin extends TemperatureDegrees{
public function __construct($value){
parent::__construct($value,new TemperatureUnitKelvin());
}
}
class TemperatureUnit{
public function setTemperatureDegrees($degrees){
$this->degrees=$degrees;
}
protected function getName(){
return substr(get_class($this),strpos(get_class($this),'Unit')+4);
}
public function add($addDegrees){
$class=$this->getName();
$method='to'.$this->getName();
return new $class($this->degrees->getValue()+$addDegrees->$method()->getValue());
}
public function addToSeft($addDegrees){
$class=$this->getName();
$method='to'.$this->getName();
$this->degrees->setValue(new $class($this->degrees->getValue()+$addDegrees->$method()->getValue()));
return $this->degrees;
}
}
class TemperatureUnitCelsius extends TemperatureUnit{
public function toCelsius(){
return $this->degrees;
}
public function toFahrenheit(){
return new Fahrenheit($this->degrees->getValue() * (9 / 5) + 32);
}
public function toKelvin(){
return new Kelvin($this->degrees->getValue() + 273.15);
}
}
class TemperatureUnitFahrenheit extends TemperatureUnit{
public function toCelsius(){
return new Celsius(($this->degrees->getValue() -32) * 5/9);
}
public function toFahrenheit(){
return new Fahrenheit($this->degrees->getValue());
}
public function toKelvin(){
return new Kelvin(($this->degrees->getValue() + 459.67) * 5/9);
}
}
class TemperatureUnitKelvin extends TemperatureUnit{
public function toCelsius(){
return new Celsius($this->degrees->getValue() - 273.15);
}
public function toFahrenheit(){
return new Fahrenheit($this->degrees->getValue() * (9/5) - 459.67);
}
public function toKelvin(){
return new Kelvin($this->degrees->getValue());
}