 |
|
|
 |
 |
|
จาก ทฤษฏี อ่ะครับ เขาจะบอกให้เราสร้าง 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();
?>
ซึ่งรูปแบบพื้นฐานจะเป็นแบบนี้...
Code (PHP) PHP_Object
<?php
class foo
{
function do_foo()
{
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo();
?>
ลองศึกษาดูนะครับ ผมเองก้อไม่ค่อยเก่งเรื่อง oop เท่าไหร่ ปกติจะเขียนแบบโบราณ ๆ แต่ทำงานได้ อิอิ (ผิดพลาดประการใดขออภัยมานะที่นี้)
ps. ขอบคุณตัวอย่างโค๊ดจาก https://www.thaicreate.com 
|
 |
 |
 |
 |
Date :
2009-03-29 16:57:48 |
By :
noomna19 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณสำหรับคำแนะนำคร้าบบ  
|
 |
 |
 |
 |
Date :
2009-03-29 17:33:19 |
By :
RangerBank |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ผมก็กำลังเริ่มเขียนแบบ oop เหมือนกัน
|
 |
 |
 |
 |
Date :
2009-03-29 20:36:11 |
By :
khunset |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผู้น้อยขอคาราวะ 1 จอก
|
 |
 |
 |
 |
Date :
2009-03-29 23:05:52 |
By :
zepherus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
การเขียน oop ก็คล้ายๆ กับ function
แต่เป็นการจัดการกับ function เป็นหมวดหมู่ และยังสามารถใช้งานได้อีกหลายๆ แบบ
ผมแนะนำ ให้หัดเขียน เป็น oop นะครับแล้วคุณจะรู้ว่า มันง่ายจริงๆ
และทำงานได้เร็วกว่าเขียนแบบ code ทั้งหน้าด้วยนะครับ
เวลามีงานคล้ายๆ กันก็จะใช้เวลาทำงานได้เร็วกว่าเดิมอีกด้วย
ลองดูนะ สู้ๆ มาเขียน oop กันดีกว่า
|
 |
 |
 |
 |
Date :
2009-03-29 23:12:59 |
By :
dr.website |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณทุกๆท่าน....... เช่นกันครับ
|
 |
 |
 |
 |
Date :
2009-03-30 11:10:57 |
By :
paejung |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อยากเขียนแบบ oop เหมียนกันอ่ะ
แต่คิดว่ามันยาก ก้อเลยเขียนแบบบ้านๆ
ถ้า web นี้มีบนเรียนสอบแบบ oop ก้อยิ่งดีเลยค่ะ
|
 |
 |
 |
 |
Date :
2009-03-31 09:20:43 |
By :
paphun |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอคาราวะ 2 จอกเลย กำลังอยากรู้พอดีค่า
|
 |
 |
 |
 |
Date :
2009-04-09 12:03:03 |
By :
ชะเอม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับเพิ่งเคยเห็นครั้งแรกจริงๆ
ไม่ค่อยได้สนใจเท่าไหร่ เพราะมอง PHP เป็น อินเตอร์พีเตอร์ ง่า
คล้ายๆ java เนอะคับ -*-
|
 |
 |
 |
 |
Date :
2009-04-10 12:52:39 |
By :
agazin |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2011-12-25 01:50:01 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องได้แนวคิด OOP ก่อนอ่ะครับ แล้วจะรู้ว่า สิ่งที่เขียๆนอยู่อะไรมันจับมามัดเป็นฟังชั่นได้
อยากดู Code ก็ลองโหลด wordpress มาแกะดู
|
ประวัติการแก้ไข 2011-12-25 02:03:51
 |
 |
 |
 |
Date :
2011-12-25 02:03:21 |
By :
evanesscaz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?php
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());
}
}
$celsius=new Celsius(-150);
echo $celsius . 'C<br/>';
echo $celsius->toFahrenheit(). 'F<br/>';
echo $celsius->toKelvin() . 'K<br/>';
echo '<br/>';
$fahrenheit=new Fahrenheit(458);
echo $fahrenheit . 'F<br/>';
echo $fahrenheit->toCelsius(). 'C<br/>';
echo $fahrenheit->toKelvin(). 'K<br/>';
echo '<br/>';
$kelvin=new Kelvin(9.15);
echo $kelvin .'K<br/>';
echo $kelvin->toCelsius(). 'C<br/>';
echo $kelvin->toFahrenheit().'F<br/>';
เขียนแบบ OOP สำหรับ แปลงค่าอุณหภูมิ
|
 |
 |
 |
 |
Date :
2012-02-11 22:02:28 |
By :
กขค |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|