สอบถามเกี่ยวกับ PHP OOP กับ ฟังก์ชันธรรมดาครับ

เริ่มโดย MiithRanDir, 12 กุมภาพันธ์ 2012, 23:40:40

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

MiithRanDir

ผมสงสัยว่าทำไมเราต้องเขียนเป็น OOP ให้มันยาว ในเมื่อเขียนฟังก์ชันธรรมดามันก็แสดงผลออกมาเหมือนกัน ยกตัวอย่างง่าย

OOP
อ้างถึง<?php
class human {

var $name;
var $sex;

function Name() {
   return $this->name;
}

function Sex() {
   return $this->sex;

}
}
$somchai = new human();
$somchai->name = 'สมชาย';
$somchai->sex = 'ชาย';
echo 'ชื่อ : ' . $somchai->Name() . ' เพศ : ' . $somchai->Sex();
?>

ผลลัพธ์
อ้างถึงชื่อ : สมชาย เพศ : ชาย


Function ปกติ
อ้างถึง<?
function Name($name) {
   return $name;
}
function Sex($sex) {
   return $sex;
}
echo 'ชื่อ : ' . Name('สมชาย') . ' เพศ : ' . Sex('ชาย');
?>

ผลลัพธ์
อ้างถึงชื่อ : สมชาย เพศ : ชาย


แล้วสมัครงานตามบริษัททั่วไปเวลาสัมภาษณ์งานนี่เข้าเน้น OOP หรือธรรมดา ท่านใดพอมีประสบการณ์บ้างครับ
[direct=https://www.มังกรฟ้าเชิญมารวย.com]ล็อตเตอรี่ออนไลน์[/direct]

Putter™

ตอนนี้ มันเป็นสมัยของ OOP แล้วมั้งครับ

ประมาณว่า มันยิดหยุ่นกว่าครับ เอาไปใช้ได้หลายๆครับ ในการทำงานในรูปแบบเดียวกัน

ส่วนที่สำภาษงานมีรุ่นพี่ผมเล่าให้ฟังว่า อยากให้เคยเขียน โปรแกรมในรูปแบบ MVC มาก่อนด้วย เรื่อง OOP นี่ก็จำเป็นมากๆครับ
[direct=https://hostings.ruk-com.in.th]Ruk-Com Hosting (IAAS)[/direct]
[direct=https://ruk-com.cloud]Ruk-Com Cloud (PAAS)[/direct]
รีวิวโฮสติ่ง Ruk-Com  จากสมาชิก THAISEO

ไม่พอใจยินดีคืนเงินเต็มจำนวนทุกบริการ

MiithRanDir

ผมสงสัยที่เค้าว่ามันยืดหยุ่นนี่แหละครับ หรือว่ามันต้องโปรแกรมยาวๆ ถึงจะเห็นผล
[direct=https://www.มังกรฟ้าเชิญมารวย.com]ล็อตเตอรี่ออนไลน์[/direct]

rukyee

ประมาณว่า Reuse Code ได้ง่ายขึ้น มั้งครับ
จากผู้รู้ งูๆ ปลาๆ
:wanwan031:
ทดสอบ#1 www.snsdthai.com
ทดสอบ#2 www.inspire-market.com

maiphp

Reuse , ปกป้องข้อมูล , etc... จำไม่ได้

ตัวอย่างนี้ น่าจะชัดเจนกว่า เรื่อง reuse
อ้างถึง
<?php
class human {

private $name;
private $sex;

function setName($name) {
   return $this->name;
}

function setSex($sex) {
   return $this->sex;

}
function output(){
echo 'ชื่อ : ' . $somchai->name . ' เพศ : ' . $somchai->sex;
}

}

$somchai = new human();
$somchai->setName('สมชาย');
$somchai->setSex('ชาย');

$somsri = new human();
$somsri->setName('สมศรี');
$somsri->setSex('หญิง');

$somchai->output();
echo '<br />';
$somsri->output();
?>
- รับทำพัฒนาเว็บ PHP, MySQL, HTML5, CSS3, Web Responsive, Laravel Framework
- รับแปลง JPG, PSD เป็น HTML
My blog [direct=http://chonable.com/]ChonAble[/direct]

LifeGood

ถ้าคุณได้ทำเว็บระบบใหญ่ ๆ จะเข้าใจว่า OOP สำคัญยังไงครับ มันเป็นเรื่องของความปลอดภัยการนำกลับมาใช้ใหม่และง่ายต่อการแก้ไข แต่ถ้าจะเขียนแค่สคริปเพื่อใช้งานเฉพาะทางเล็ก ๆ น้อย ๆ ก็ไม่ต้องใช้ OOP ครับ สำหรับบริษัทสมัยนี้ต้องเขียน OOP เป็นครับ บางที่ต้องใช้ framework ด้วยซ้ำ ซึ่งแน่นอนว่ายังไงก็หนี OOP ไม่พ้น
รับงาน PHP Programming ฟรีแลนซ์ จากโปรแกรมเมอร์ประสบการณ์กว่า 8 ปี ชำนาญเฟรมเวิร์ค CI ปรับแต่งได้ทุก CMS รับทั้งงานเล็กและงานใหญ่ในราคาเพื่อนฝูง รับทีละงาน ไม่พอกหางหมู เสร็จตรงเวลาแน่นอน แจ้งความคืบหน้าและอัพเดทความคืบหน้าของงานทุก 3 วัน สนใจ PM มาครับ!!!

chinjung01

ขอตอบในฐานะเพิ่งเรียน JAVA มา จำได้บ้างไม่ได้บ้าง

สำหรับ code สั้นๆใช้แบบธรรมดาจะดีกว่า
แต่ชีวิตจริงๆ มันไม่ใช่โคดสั้นๆ มันมีเป็นพันๆ หมื่นๆบรรทัดบรรทัด

แทนที่จะให้คน 1 คนเป็นคนเขียน ก็แบ่งกันเขียนคนล่ะส่วน จะได้เสร็จไวๆ

ยืดหยุ่นกว่าหากเกิดในกรณีที่ว่าโคดบางส่วนอันเก่าสามารถไปประยุกต์ใช้กับอันใหม่ได้

แทนที่นั่งหาหรือคัดลอกเราก็สามารถเรียกใช้มันได้เลย

อีกอย่างมันไม่ต้องเขียนโคดซ้ำหลายๆรอบ แค่อ้างอิงก็โดดไปใช้งานได้เลย


EntServ

 :P

ลองเขียนoop ดู จะรู้ว่ามันง่ายมากเวลาแก้โค๊ด เพราะเวลาเรียกใช้มันมีไม่กี่บรรทัด
รับทำระบบ Streaming Server อยากได้เว็บสตรีมมิ่งหล่อๆ ติดต่อ PM ครับ

itportal

ถ้าไม่ดีจริง คงไม่ได้รับความนิยมขนาดนี้หรอกครับ ลองดู CMS แต่ละตัวสิ
และตอนนี้บริษัทต่างๆ ที่มีการใช้ระบบขนาดกลางถึงใหญ่ ก็เข้าสู่ OOP กันหมดแล้วครับ
[direct=http://phuketecho.blogspot.com/]ภูเก็ต[/direct] | [direct=http://www.phuketoriginalseafood.com]Phuket Seafood Restaurant[/direct] | [direct=http://www.phuketecho.com]Phuket classifieds[/direct]

afterdead

ผมคนนึงที่เวลาเขียน oop ช้ากว่า เขียนปกติ ทำใจไม่ค่อยได้ 55+  :-[
ขายเว็บอ่านการ์ตูน Manga Traffic 4000+/days PageView 36,000/ days PM
VPS CHEAP
[direct=https://www.digitalocean.com/?refcode=66904e5523f1]สุดยอด ! CLOUD VPS SSD 20 GB RAM 512 จ่ายผ่าน Paypal หรือ บัตรเครดิต ! แค่ $5.0 คุ้มกว่านี้มีที่ไหน มีสิงค์โปร์ ใช้กับเว็บไทยสุดยอดขอบอกๆ[/direct]
Unlimited & Unlimited Hosting.
HOST GATOR COUPONS ลด 25% ใส่คูปอง "tsbgator25off" [direct=https://signup.hostgator.com/hg4/shared-29-36-tsbgator25off?]>>>Hatchling Plan<<<[/direct] |

ลุงหมี

เป็นเรื่องของ Reuse และการทำงานเป็นทีมด้วยะครับ การพัฒนาแบบ OOP จะได้เปรียบในระยะยาว ... แต่ถ้าเราเขียนใหม่หมดทุกครั้งเนี่ยก็ไม่เห็นความแตกต่างหรอกครับ เหนื่อยกว่าด้วย :wanwan020:

9neo

80% ตามบริษัททั่วไป ตอนนี้เน้น oop กันแทบทั้งนั้น เพราะส่วนใหญ่ใช้ framework ในการทำงาน

หายังต้องทำงานร่วมกับผู้อื่น หรือใช้ library จากต่างประเทศ ไม่ว่าจะยังใง ไม่มีทางหนีมันพ้นแน่นอนครับ

oop จะมีประโยชน์เมื่อคนทำเข้าใจคอนเซ็ปและประโยชน์ของมัน

ไปถามเวบบอร์ดใหน คำตอบก็จะซ้ำๆ ครับ reuse ใช้งานง่าย ดูโค๊ดง่าย แก้ใขง่าย

แต่มั่นใจเลยว่า หลายคนจะมองประโยชน์มันไม่ออก จนกว่าจะเข้าใจและเขียนออกมาใช้งานจริงๆ

ก่อนหน้านี้เขียน function อยู่เหมือนกัน พอเข้าใจคอนเซ็ป oop แล้ว แทบจะรื้อโค๊ดใหม่ที่เขียนๆ ไว้หมดเลยทีเดียว  :-X


ohmohm

ขอเพิ่มเติมเกี่ยวกับ การปกป้องข้อมูล ก็แก้ที่ class ไม่ต้องประกาศข้อมูลเพิ่มเติมข้างนอก ป้องกันข้อมูลนั้นโดนแก้จากที่อื่นเพราะเปิดโล่งโต้งด้วย
การสืบทอด ถ้าเพิ่มเติมคุณสมบัติ ก็เอาของเก่ามาใช้ได้
อ้างถึง
<?php
class human {

private $name;
private $sex;
private $basket = Array();

function setName($name) {
   return $this->name;
}

function setSex($sex) {
   return $this->sex;
}

function popbasket ( ) {
   return array_pop ( $this->basket  ) ;
}


function pushbasket ( $b ) {
   return array_push ( $this->basket  , $b ) ;
}

function output(){
echo 'ชื่อ : ' . $somchai->name . ' เพศ : ' . $somchai->sex;
}

}

class engineer extends human {
    var $yearskill;
    function setyearskill ($y) {
        $this->yearskill = $y;
    }
    function getyearskill () {
        return $this->yearskill = $y;
    }
}

$somchai = new human();
$somchai->setName('สมชาย');
$somchai->setSex('ชาย');

$somsri = new human();
$somsri->setName('สมศรี');
$somsri->setSex('หญิง');

$somchai->output();
echo '<br />';
$somsri->output();
?>


thai2ads

หา framework ดีๆสักตัวมาใช้งานผมก็ว่าดีเหมือนกันนะ ^ ^ จะได้ไม่ต้องเขียนใหม่
รับทำเว็บไซต์ เขียนระบบต่างๆตามที่ต้องการ
รับทำเว็บไซต์ | ทองคำราคาวันนี้

sgio

อ้างถึงจาก: thai2ads ใน 13 กุมภาพันธ์ 2012, 20:54:13
หา framework ดีๆสักตัวมาใช้งานผมก็ว่าดีเหมือนกันนะ ^ ^ จะได้ไม่ต้องเขียนใหม่

แม่นแล้วเป็นผม ผมแนะนำ Yii นะ

ส่วนตัวผมชอบเขียน oop
Reuse ได้หรือไม่ ไม่รู้
รู้แต่เขียนแล้ว code ออกมาสวย
ปวดหัวดี แต่ชอบ อิๆ

SpiderManKing

ลองยกตัวอย่างการเขียน แบบ OOP ในการแก้ปัญหา

จงแปลงค่าต่อไปนี้
-150 Celsius แปลงเป็น Fahrenheit ,Kelvin
123 Fahrenheit แปลงเป็น Celsius ,Kelvin
9.5 Kelvin  แปลงเป็น Celsius ,Fahrenheit

จากนั้นนำค่าทั้งหมดมาบวกกัน ลบด้วย 1000 Fahrenheit  และ แปลงคำตอบออกมาเป็น Celsius ,Fahrenheit  และ Kelvin

<?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 sub($subDegrees){
      return $this->temperatureUnit->sub($subDegrees);
   }
   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 sub($subDegrees){
      $class=$this->getName();
      $method='to'.$this->getName();
      return new $class($this->degrees->getValue()-$subDegrees->$method()->getValue());
   }
   
   public function subToSeft($subDegrees){
      $class=$this->getName();
      $method='to'.$this->getName();
      $this->degrees->setValue(new $class($this->degrees->getValue()-$subDegrees->$method()->getValue()));
      return $this->degrees;
   }
   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(123);
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/>';

$result=$celsius->add($celsius)
            ->add($celsius->toFahrenheit())
            ->add($celsius->toKelvin())
            ->add($fahrenheit)
            ->add($fahrenheit->toCelsius())
            ->add($fahrenheit->toKelvin())
            ->add($kelvin)
            ->add($kelvin->toCelsius())
            ->add($kelvin->toFahrenheit())
            ->sub(new Fahrenheit(1000));
echo '<br/>';      
echo $result . 'C<br/>';
echo $result->toFahrenheit(). 'F<br/>';
echo $result->toKelvin() . 'K<br/>';

-150C
-238F
123.15K

123F
50.5555555556C
323.705555556K

9.15K
-264C
-443.2F

-1778.11111111C
-3168.6F
-1504.96111111K









guitarnote

ผมว่า oop มันดีตรงที่มันสืบทอดได้ด้วยนะครับ คือฟังก์ชั่นอันเดิมเราบะต้องรู้ว่ามันทำงานยังไง สืบทอดมาแล้วเอามาใช้ หรือสืบทอดมาเอามาปรับปรุงแก้ไขได้

mean

จริงๆ OOP มันง่าย ในการเรียกใช้ครับ
Static Function ธรรมดา ทำงานได้เร็ว และเข้าถึงได้ดีกว่า OOP
แต่ OOP สามารถกำหนด การเข้าถึง เรียกใช้งาน Function ได้ สืบทอดได้
OOP ง่ายในการแบ่ง ประเภท function หรือ ระบุ Class หรือ บ่งบอกที่มา ของ Function

ลองฝึกเขียน Class ดูครับ แล้วจะชอบ และเข้าใจมากขึ้น
เพราะการทำงานบางอย่าง Function ไม่ตอบโจทย์ครับ


[direct=https://www.ireallyhost.com/]บริการโดเมน, เว็บโฮสติ้ง, SSL Certificate[/direct]
[direct=https://www.ireallyhost.com/ssl]บริการ SSL Certificate , HTTPS สำหรับเว็บไซต์[/direct]
[direct=https://www.ireallyhost.com/]บริการ Web Hosting Free! Domain name ตลอดอายุบริการ [/direct]
[direct=https://www.ireallyhost.com/domain]บริการ จดโดเมน, Free DNS Service, Domain Forwarding, จัดการโดเมนได้เอง Domain Control Panel[/direct