1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Bài giảng lập trình web nâng cao chương 4 trường đh văn hiến

20 4 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

24/05/2021 Lập trình web nâng cao Chương – Lập trình hướng đối tượng 01 Giới thiệu 02 Các vấn đề hướng đối tượng 03 Lớp abstract lớp interfaces 04 05 24/05/2021 Lập trình web nâng cao Giới thiệu  OOP (Object Orient Programming) revolves around the concept of grouping code and 01 data together in logical units called classes This process is usually referred to as 02 encapsulation, or information hiding, since its goal is that of dividing an application into separate entities whose internal components can change without altering their external interfaces (ref: page 132 of ebook “phparchitects Zend PHP Certification Study Guide”) 04  Programming techniques may include features such as 05 abstraction, encapsulation, polymorphism, and inheritance 24/05/2021 Lập trình web nâng cao Các vấn đề OOP PHP  Cú pháp khai báo lớp: Declaring a Class 02 class { // Your code is here … }  Ví dụ: 04 class foo { const BAR = "Hello World"; } echo foo::BAR; 05 24/05/2021 Lập trình web nâng cao Các vấn đề OOP PHP Declaring a Class 02 04 05 24/05/2021  Cú pháp khai báo lớp kế thừa: class a { function test(){ echo "a::test called";} function func(){echo "a::func called";} } class b extends a { function test(){echo "b::test called";} } class c extends b { function test(){parent::test();} } class d extends c { function test(){b::test();} }  Cú pháp xác định lớp đối tượng: if ($obj instanceof MyClass) { echo "\$obj is an instance of MyClass"; } Lập trình web nâng cao Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 04 05 24/05/2021  Cú pháp tạo đối tượng: $myClassInstance = new myClass(); Lưu ý: đối tượng PHP sử dụng theo dạng tham chiếu  Ví dụ: $myClassInstance = new myClass(); $copyInstance = $myClassInstance(); // Cả biến $myInstance $copyInstance trỏ tới đối tượng thuộc myClass 0fx01 $myClassInstance myClass $copyInstance Lập trình web nâng cao Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 04 05 24/05/2021  Phương thức thuộc tính: class myClass { function myFunction() { echo "You called myClass::myFunction"; } } // Access methods of class myClass $obj = new myClass(); $obj -> myFunction(); $myClassInstance myClass $copyInstance Lập trình web nâng cao Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 04 05 24/05/2021  Con trỏ $this: class myClass { function myFunction($data) { echo "The value is $data"; } function callMyFunction($data) { // Call myFunction() $this->myFunction($data); } } $obj = new myClass(); $myClassInstance $obj->callMyFunction(123); myClass $copyInstance Lập trình web nâng cao Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 Constructors 04 05 24/05/2021  Cú pháp hàm khởi tạo: class foo { function construct() { // PHP new style constructor echo METHOD ; } function foo() { // PHP style constructor } } $myClassInstance new foo(); myClass $copyInstance Lập trình web nâng cao Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 Constructors Destructors 04 05 24/05/2021  Cú pháp hàm hủy: class foo { function construct() { echo METHOD PHP_EOL; } function destruct() { echo METHOD ; } } new foo(); $myClassInstance myClass $copyInstance Lập trình web nâng cao 10 Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02  Phạm vị truy cập: Key public The resource can be accessed from any scope protected private The resource can only be accessed from within the class where it is defined and its descendants The resource can only be accessed from within the class where it is defined final The resource is accessible from any scope, but cannot be overridden in descendant classes Constructors Destructors Visibility 04 05 24/05/2021 Visibility  Ví dụ: $myClassInstance myClass $copyInstance Lập trình web nâng cao 11 Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 Constructors Destructors Visibility 04 05 24/05/2021  Ví dụ 1: kết đoạn lệnh sau class foo { public $foo = 'bar'; protected $baz = 'bat'; private $qux = 'bingo'; function construct(){ var_dump(get_object_vars($this)); } } class bar extends foo { function construct(){ var_dump(get_object_vars($this)); } } new foo(); new bar(); $myClassInstance myClass $copyInstance Lập trình web nâng cao 12 Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 Constructors Destructors Visibility 04 05 24/05/2021  Ví dụ 2: kết đoạn lệnh sau class foo { public $foo = 'bar'; protected $baz = 'bat'; private $qux = 'bingo'; function construct(){ $this->foo="pig"; var_dump(get_object_vars($this)); echo ""; } } class bar extends foo { function construct(){ var_dump(get_object_vars($this)); echo ""; } } new foo(); new bar(); $myClassInstance myClass $copyInstance Lập trình web nâng cao 13 Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 Constructors Destructors Visibility 04 05 24/05/2021  Ví dụ 3: kết đoạn lệnh sau class foo { public $foo = 'bar'; protected $baz = 'bat'; private $qux = 'bingo'; function construct(){ var_dump(get_object_vars($this)); echo ""; } } class baz { function construct() { $foo = new foo(); var_dump(get_object_vars($foo)); echo ""; } } new foo(); new baz(); $myClassInstance myClass $copyInstance Lập trình web nâng cao 14 Các vấn đề OOP PHP Declaring a Class Instantiating an Object 02 Constructors Destructors Visibility Constants, Static Methods and Properties 04 05 24/05/2021  Cú pháp khai báo biến phương thức tĩnh: class foo { static $bar = "bat"; static public function baz(){ echo "Hello World"; } } echo foo::$bar.""; foo::baz(); // output: bat Hello world  Cú pháp khai báo lớp: class foo { const BAR = "Hello World"; } echo foo::BAR; // output: Hello world $myClassInstance myClass $copyInstance Lập trình web nâng cao 15 Lớp Abstract lớp Interface Interface  Go  … a Giới thiệu 01 02 Animal 04 05 24/05/2021 Interface  Go  Run  Fly  Swim … Chim máy bay có interface Fly cách thức hoạt động Fly khác hoàn toàn Interface  Run  … Transport Lập trình web nâng cao 16 Lớp Abstract lớp Interface b Lớp trừu tượng Khái niệm  Lớp trừu tượng lớp cha cho tất lớp có chất Do lớp dẫn xuất (lớp con) kế thừa từ lớp trừu tượng  Lớp trừu tượng không cho phép tạo instance (không thể tạo đối tượng thuộc lớp đó)  Cú pháp khai báo lớp trừu tượng abstract class { [properties … ] abstract function func_name(…); public function func_name(…); } Lớp Abstract lớp Interface b Lớp trừu tượng  Ví dụ abstract class DataStore_Adapter { private $id; abstract function insert(); abstract function update(); public function save(){ if (!is_null($this->id)){ $this->update(); } else { $this->insert(); } } } class PDO_DataStore_Adapter extends DataStore_Adapter { public construct($dsn){ // } function insert(){ // } function update(){ // } } Lớp Abstract lớp Interface c Lớp interface Khái niệm  Lớp interface xem mặt nạ cho tất lớp cách thức hoạt động khác chất  Lớp dẫn xuất kế thừa từ nhiều lớp interface để bổ sung đầy đủ cách thức hoạt động (đa kế thừa Multiple inheritance)  Cú pháp khai báo lớp interface interface class { … } Lớp Abstract lớp Interface c Lớp interface  Ví dụ • interface DataStore_Adapter { public function insert(); public function update(); public function save(); public functionnewRecord($name = null);} class PDO_DataStore_Adapter implements DataStore_Adapter { public function insert(){ // } public function update(){ // } public function save(){ // } public function newRecord($name = null){ } }

Ngày đăng: 25/11/2023, 14:04