02 04 Giới thiệu 05 OOP Object Orient Programming revolves around the concept of grouping code and data together in logical units called classes.. This process is usually referred to a
Trang 124/05/2021 Lập trình web nâng cao 1
Trang 2Giới thiệu
01
Các vấn đề cơ bản hướng đối tượng trong
02
03
04
05
Lớp abstract và lớp interfaces
03
Trang 302
04
Giới thiệu
05
OOP (Object Orient Programming) revolves around the concept of grouping code and
data together in logical units called classes This process is usually referred to as 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 5 Certification Study Guide”)
Programming techniques may include features such as
abstraction, encapsulation, polymorphism, and inheritance.
Trang 404
Các vấn đề cơ bản OOP trong PHP
05
1 Declaring a Class
Cú pháp khai báo lớp:
Ví dụ:
class <Tên_lớp>{
// Your code is here
… }
class foo {
const BAR = "Hello World";
} echo foo::BAR;
Trang 504
Các vấn đề cơ bản
OOP trong PHP
05
1 Declaring a Class
Cú pháp khai báo lớp kế thừa:
Cú pháp xác định lớp đối tượng:
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();}
}
if ($obj instanceof MyClass) {
echo "\$obj is an instance of MyClass";
}
Trang 604
Các vấn đề cơ bản
OOP trong PHP
05
1 Declaring a Class
2 Instantiating an
Object
Cú pháp tạo đối tượng:
Lưu ý: các đối tượng trong PHP được sử dụng
theo dạng tham chiếu
Ví dụ:
$myClassInstance = new myClass();
$myClassInstance = new myClass();
$copyInstance = $myClassInstance();
// Cả 2 biến $myInstance và $copyInstance cùng trỏ tới một đối tượng thuộc myClass
$myClassInstance
$copyInstance
myClass
0fx01
Trang 704
Các vấn đề cơ bản
OOP trong PHP
05
1 Declaring a Class
2 Instantiating an
Object
$myClassInstance
$copyInstance
myClass
Phương thức và thuộc tính:
class myClass {
function myFunction() {
echo "You called myClass::myFunction";
} }
// Access methods of class myClass
$obj = new myClass();
$obj -> myFunction();