Object Oriented PHP Step 1-5

Object Oriented PHP Step 1-5

มาเริ่มต้นศึกษาการเขียน PHP แบบ Object Oriented กันครับ ซึ่งจะเป็นแบบที่แพร่หลายใน PHP5 อย่างน้อยก็รองรับอนาคตที่จะมาของ PHP6 ด้วย อย่ายึดติดแต่แบบเดิมๆ

ในการเรียนรู้ครั้งนี้ เพื่อให้ง่าย ก็จะมีเรื่อง PHP Basic ซึ่งประกอบด้วย Function ,variable, condition และ loop

เพื่อให้ง่าย และเป็นลำดับตามความเข้าใจ จะแบ่งเป็น 22 ขั้นตอน สามารถค่อยๆทำตามได้เลย

 

STEP 1:
เริ่มจากการสร้าง ไฟล์  PHP จำนวน 2 ไฟล์

index.php
class_lib.php

เนื่องจาก OOP จะมีลักษณะเป็น module ทำให้ Object Oriented PHP แบ่งออกมาเป็นไฟล์ได้ แล้วเราก็เอามารวมกันได้ ในตอนที่ประมวลผล ด้วยคำสั่ง "include"

ในจุดนี้ เราจะใช้ class_lib.php  เป็นหลักในการเขียน PHP OOP

สิ่งที่เราจะสร้างขึ้นมา ใน OOP จะเรียกกันว่า Class ซึ่ง Class เปรียบเหมือนแม่แบบ ในการสร้าง Object ต่อไป


STEP 2:
สร้าง class แบบง่ายๆ (ใน class_lib.php)

โดย class จะมีส่วนประกอบของ function , variable และ code ต่างๆ ในการเขียน PHP script  หรือว่า การทำ PHP library คุณก็จะต้องสร้าง PHP Class ของตัวคุณเอง ขึ้นมาใช้

การประกาศ Class ของคุณขึ้นมาใช้เอง โดยเริ่มต้นคำ keyword  ว่า 'class' แล้วตามด้วยชื่อ class ที่ตั้งขึ้นเอง

<?php   class person {     } ?>

คุณต้องเปิดและ ปิด  class ด้วยเครื่องหมายปีกกา เหมือนกับตอนที่เขียน function เลย

STEP 3:
ใส่ data เข้าไปใน class

Class เปรียบเหมือน ต้นฉบับของ PHP Object - แต่ว่าทำได้มากกว่านั้น หลักๆที่แตกต่างจาก function ก็คือ class สามารถใส่ข้อมูล (variable) และ ชุดของ function ได้ ซึ่งเราเรียกว่า Object 

เมื่อคุณได้สร้าง variable ใน class เราจะเรียกว่า 'property'

<?php  class person {  var $name;  } ?>

variable ใน class เราจะเรียกว่า 'property'

STEP 4:
เพิ่ม function/method ลงใน class

ก็เหมือนกับ variable เราสามารถตั้งชื่อต่างๆลงใน class ได้ (ที่เราเรียกว่า property) , funtion ชื่อต่างๆที่เราสร้างใส่ใน class  เราจะเรียกว่า 'method'

method ของ class ก็ต่างที่จะมี ข้อมูลหรือ property ของตัวเองได้ด้วยเช่นกัน

<?php  class person {  var $name;  function set_name($new_name) {  $this->name = $new_name;  }  function get_name() {  return $this->name;  }  } ?>

ใน class เราจะเรียก variable ว่า property และเรียก function ว่า method

STEP 5:
เรียก(get) และ สร้าง(set) function

method ที่เห็นนี้ คือ OOP ตามปกติ ซึ่งจะพบได้ในทุกภาษา (รวมถึง Java และ Ruby) เมื่อคุณได้ 'set' และ 'get' property ใน class

<?php  class person {  var $name;   function set_name($new_name) {   $this->name = $new_name;     }    function get_name() {  return $this->name;  } }  ?>

การสร้าง และ เรียก จะต้องอ้างอิงตามชื่อของ property ที่สร้างให้ถูกต้อง

จนถึงตอนนี้ หากมี PHP Programmer คนอื่นต้องการใช้ Object ที่คุณสร้าง เค้าก็จะรู้ว่า คุณมี function/method set_name() และ get_name() ให้เรียกใช้ได้ ซึ่งมี property ทีชื่อ 'name'

อ่าน OO PHP Step By Step  6-11

บทความทั้งหมดนี้ แปล และเรียบเรียงมาจาก  http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php
This article translate and remade from http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php

[PRINT VERSION] เขียน: 2010-07-08 09:15:44 แก้ไข : 2010-07-08 09:15:44 อ่าน : 10669
tag : PHP5, Object Oriented PHP, OO PHP, PHP OOP, Object Oriented Programming,OOP, PHP,เขียนเว็บ Object Oriented, ทำเว็บ Object Oriented

Comment

Comment
BeYourCyber - 08/07/2010 08:15:44 -
ร่วมพูดคุยในหัวข้อของบทความ Object Oriented PHP Step 1-5
มาเริ่มต้นศึกษาการเขียน PHP แบบ Object Oriented กันครับ ซึ่งจะเป็นแบบที่แพร่หลายใน PHP5 อย่างน้อยก็รองรับอนาคตที่จะมาของ PHP6 ด้วย อย่ายึดติดแต่แบบเดิมๆ
Guest - 20/02/2011 22:52:43 - 110.164.69.66
It is a shame that you just translate Stefan Mischook article and put your name as creator. See there...

http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php
BeYourCyber - 21/02/2011 14:19:46 - 58.10.87.95
Your right this article not my own, I translate it and remade it from that page for thai people who interest to learn php oop.

Now i put right credit and back link to original page.

If Stefan Mischook tell me for delete this article(or all of this series) for any reason i will delete it immediately.

Don't worry for anything. :)
Guest - 13/09/2011 13:14:38 - 124.121.68.239
ขอบคุณครับ