งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

สำนักวิชาเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยนเรศวร พะเยา

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "สำนักวิชาเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยนเรศวร พะเยา"— ใบสำเนางานนำเสนอ:

1 สำนักวิชาเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยนเรศวร พะเยา
Object-Oriented PHP กนกวรรธน์ เซี่ยงเจ็น สำนักวิชาเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยนเรศวร พะเยา

2 เนื้อหา หลักการของ Object-Oriented
การสร้าง Class, Attribute และ Operation ใน PHP การเรียกใช้ Class Attribute การเรียกใช้ Class Operation การถ่ายทอดคุณสมบัติ Inheritance การออกแบบ Class

3 หลักการของ Object-Oriented
Class and Object Polymorphism Inheritance

4 Class คือแม่แบบของ Object ที่ประกอบด้วย
Class and Object Class คือแม่แบบของ Object ที่ประกอบด้วย คุณสมบัติ (Attribute): เช่นตัวแปรต่างๆ ที่ประกาศขึ้นใช้งาน วิธีการดำเนินการ (Operation): เช่น ฟังก์ชันการทำงานต่างๆ ที่กระทำกับ คุณสมบัติที่นิสิตกำหนดขึ้นมา

5 Inheritance Inheritance คือ คุณสมบัติในการถ่ายทอดลักษณะ และ Member จาก Class หนึ่งที่อยู่เหนือกว่า (Super class) ไปยังอีก Class หนึ่งที่อยู่ระดับล่าง (Sub class) โดย Class ที่รับการถ่ายทอด จะมีลักษณะเช่นเดียวกัน Class ต้นแบบทุกประการ โดยที่เราสามารถปรับปรุง เปลี่ยนแปลงลักษณะและ Member จาก Class ต้นแบบ ให้มีความสามารถใหม่ๆ ขึ้นมา ช่วยให้ประหยัดเวลาในการพัฒนา

6 หรือ "Parent Class" หรือ "Child Class" Parent Class Child Class
Class ต้นแบบ เรียกว่า "Base Class" หรือ "Parent Class" Class ที่รับการถ่ายทอด เรียกว่า "Derived Class" หรือ "Child Class"

7 Person Nurse Doctor Student Class ต้นแบบ คือ Person Class ที่รับการถ่ายทอด คือ Student, Nurse, Doctor ซึ่งจะมีคุณลักษณะเหมือน Person

8 Inheritance ข้อดี ข้อเสีย
สามารถนำ code กลับมาใช้ใหม่ได้ โดยไม่ต้องเขียนใหม่ สามารถแก้ไขที่ super class ที่เดียว โดยไม่ต้องแก้ไขทุกๆ sub class ที่ได้รับการ extend มา ลดความซับซ้อนของโปรแกรม ข้อเสีย ถ้าออกแบบไม่ดี มีผลกระทบที่ร้ายแรงในภายหลัง

9 การสร้าง Class, Attribute และ Operation ใน PHP
class classname { var $attribute_name; function func_name(){ } // end function } // end class การประกาศเพื่อสร้าง class สร้าง attribute ต้องมี var สร้าง operation

10 ตัวอย่าง การประกาศเพื่อสร้าง class สร้าง operation class student {
var $student_id; function show_student_id(){ } // end function } // end class การประกาศเพื่อสร้าง class สร้าง attribute ต้องมี var สร้าง operation

11 Constructor เป็นฟังก์ชันที่จะถูกเรียกใช้งานโดยอัตโนมัติ เมื่อมีการสร้าง Object ใหม่ โดยที่ Constructor มีรูปแบบคือ หรืออาจกำหนดชื่อของฟังก์ชัน ให้ตรงกับชื่อ Class ก็ได้ เรานิยมใช้สำหรับกำหนดค่าเริ่มต้นให้กับ attribute หรือ method function __construct(){ } รูปแบบ

12 Example: __construct()
class student { var $student_id; function __construct($param){ echo "Constructor with parameter $param"; } // end function } // end class การประกาศเพื่อสร้าง class สร้าง attribute สร้าง constructor

13 Destructor เป็นฟังก์ชันที่จะถูกเรียกใช้งานโดยอัตโนมัติ ก่อนการทำลาย Class ที่ใช้งานอยู่ โดยที่ Destructor มีรูปแบบคือ จะถูกเรียกใช้เมื่อมีการ unset ตัวแปร Class ที่เราสร้างขึ้น function __destruct(){ } รูปแบบ

14 Example: __destruct()
class student { var $student_id; function __destruct($param){ echo "You are Destroy Class"; } // end function } // end class การประกาศเพื่อสร้าง class สร้าง attribute สร้าง destructor

15 Example: construct & destruct
class student{ function __construct($param){ echo "Construct Class for $param <br/>"; } // end constructor function __destruct(){ echo "Destroy Class!!"; } // end destructor } // end class $stu = new student('uthai'); // create instance unset($stu); // destroy class Construct Class for uthai Destroy Class!!

16 การเรียกใช้ Class Attribute
ในการใช้งาน Class เมื่อเราต้องการที่จะอ้างอิง Attribute ที่อยู่ภายใน Class สามารถทำได้ โดยใช้ Pointer พิเศษที่ชื่อว่า $this ชี้ไปที่ชื่อของ Attribute ที่เราต้องการ var $attribute_name; . . . $this -> attribute_name; รูปแบบ

17 การเรียกใช้ Class Operation
การเรียกใช้งาน Class Operation จะทำได้ก็ต่อมีการเรียกใช้งานผ่าน instance ที่เราสร้างขึ้นมาจาก Class

18 Example: Class Attribute & Operation
class student{ var $student_id; function __construct($param){ $this -> student_id = $param; } // end constructor function show_id(){ echo "student id = " . $this -> student_id; } // end function } // end class $stu = new student( ); // create instance $stu -> show_id(); // show student id student id = Class Attribute Class Operation

19 การถ่ายทอดคุณสมบัติ Inheritance
เป็นการถ่ายทอด Attribute และ Operation จาก Class แม่แบบ โดยเราต้องใช้คำสั่ง extends ตามหลังชื่อ Class แล้วปิดท้ายด้วยชื่อ Class ต้นแบบ สร้าง Class student โดยถ่ายทอด Attribute และ Operation จาก Class person class student extends person { } รูปแบบ

20 Example: Inheritance Hello, Uthai ShiangJan class person{
var $firstname; var $lastname; function __construct($fname, $lname){ $this -> firstname = $fname; // assign value to firstname $this -> lastname = $lname; // assign value to lastname } // end function function show_detail(){ echo "Hello, " ; echo $this -> firstname . " " . $this -> lastname; } // end class $detail = new person("Uthai", "ShiangJan"); $detail -> show_detail(); Hello, Uthai ShiangJan

21 Example: Inheritance Hello, Uthai ShiangJan Person Student
class person{ var $firstname; var $lastname; function __construct($fname, $lname){ $this -> firstname = $fname; // assign value to firstname $this -> lastname = $lname; // assign value to lastname } // end function function show_detail(){ echo "Hello, " ; echo $this -> firstname . " " . $this -> lastname; } // end class class student extends person{ // inheritance $detail = new student("Uthai", "ShiangJan"); $detail -> show_detail(); Hello, Uthai ShiangJan Person Student

22 Overriding ในการ Inheritance Class มาเพื่อใช้งานบางครั้งเราจำเป็นต้องมีการเปลี่ยนแปลง (Modify) function การทำให้ให้สอดคล้องกับ Class ใหม่ที่เราสร้างขึ้น การทำ Overriding คือการสร้าง Function ใน Sub Class ที่มีชื่อเหมือน Function ของ Class แม่แบบ เพื่อให้ทำงานที่แตกต่างไปจาก Function ของ Class แม่แบบ

23 การออกแบบ Class โดยทั่วไปเราจะนำ Class แทน Object ที่มีอยู่ในชีวิตจริง เช่น หากเราต้องการออกแบบ E-Commerce เราก็ควรแบ่งส่วนประกอบของ E-Commerce เป็นส่วนๆ เช่น Shopping cart ก็แยกออกมาสร้างเป็น Class เพื่อให้สามารถนำเอา Shopping cart ไปใช้งานได้กับเว็บ E-Commerce อื่นๆ ได้อีก

24 Example: Overriding Hi World Hello World class person{
function show_detail(){ echo "Hello World " ; } // end function } // end class class student extends person{ $detail = new student(); $detail -> show_detail(); class person{ function show_detail(){ echo "Hello World " ; } // end function } // end class class student extends person{ echo “Hi World " ; $detail = new student(); $detail -> show_detail(); Hi World Hello World


ดาวน์โหลด ppt สำนักวิชาเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยนเรศวร พะเยา

งานนำเสนอที่คล้ายกัน


Ads by Google