Trong PHP, các khối giao diện khai báo một tập hợp các hàm được định nghĩa với một lớp để thực hiện giao diện này. Một lớp có thể mở rộng nhiều hơn một giao diện, do đó, chúng ta có thể mô phỏng nhiều kế thừa trong PHP. Hãy để chúng tôi xem tất cả về nó, chi tiết với bài viết này
Lớp PHP triển khai Giao diện
Trong khi triển khai một giao diện với một lớp PHP, chúng ta phải định nghĩa tất cả các chức năng được khai báo trong giao diện đó, không thay đổi đối số, kiểu trả về, công cụ sửa đổi truy cập hoặc bất kỳ thứ gì
Ví dụ: đoạn mã sau xử lý giao diện và lớp PHP để chỉ ra cách triển khai giao diện cho một lớp
$v] {
$menu_bar .= "" . $menu_array[$k]["menu_caption"] . " ";
}
return $menu_bar;
}
public function getContent[$page]
{
$content = "Welcome to the " . ucwords[$page] . " page";
return $content;
}
}
?>
Trong ví dụ trên, chúng tôi đã tạo một giao diện PHP có tên là UserInterface và giao diện này được triển khai bởi một lớp WebApp bằng cách sử dụng từ khóa implements. Chúng tôi có thể triển khai giao diện này, bất cứ nơi nào chúng tôi muốn cùng một bộ thành phần giao diện người dùng là menu và nội dung cho ứng dụng web của chúng tôi
Trong khi triển khai giao diện trên, hai hàm được khai báo tại UserInterface đã được định nghĩa trong lớp triển khai giao diện đó
Trong khi xác định, nếu bất kỳ một trong các hàm đó hoặc bất kỳ chữ ký nào, như đối số, kiểu trả về bị thay đổi, thì nó sẽ gây ra một số lỗi PHP. Ví dụ: nếu lớp WebApp không có định nghĩa getContent[] thì lỗi sẽ là,
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
Giao diện mở rộng Giao diện
Trong PHP, một giao diện có thể mở rộng một giao diện khác và do đó nó kế thừa tất cả các chức năng được khai báo với giao diện này. Đối với điều đó, chúng ta nên sử dụng từ khóa mở rộng, như chúng ta đã sử dụng để kế thừa các lớp
Ví dụ, chúng ta hãy thêm một giao diện tên là Widget, với chương trình trên, trong đó hàm getPopularTags[] được khai báo. Bây giờ, chúng ta sẽ mở rộng giao diện Widget này cho UserInterface
Bằng cách mở rộng giao diện Widget, nghĩa là UserInterface được mở rộng thêm một khai báo hàm cho getPopularTags[]. Bây giờ, để triển khai UserInterface, một lớp PHP cũng phải định nghĩa getPopularTags[]
Đa kế thừa trong PHP
Như chúng ta đã thấy trong bài viết về kế thừa PHP, PHP chỉ hỗ trợ kế thừa đơn. Tuy nhiên, bằng cách sử dụng thuộc tính mở rộng giao diện PHP này, chúng ta có thể mô phỏng nhiều kế thừa
Một giao diện mở rộng hai giao diện, vì một lớp kế thừa hai lớp cha nơi hỗ trợ nhiều kế thừa. Ví dụ, trong chương trình sau, UserInterface mở rộng cả hai giao diện có tên là Advertisement và Widget để kế thừa các hàm đã khai báo với chúng
Nếu bất kỳ lớp PHP nào triển khai Giao diện người dùng phải có định nghĩa cho tất cả các chức năng được khai báo trong Giao diện người dùng và cả trong các giao diện khác, đó là Widget, Advertisement, thì nó sẽ mở rộng bằng
Hằng số giao diện PHP
Trong PHP, các hằng số được định nghĩa bên trong khối giao diện, bằng cách sử dụng từ khóa const. Ví dụ, hằng số Giao diện PHP có thể được định nghĩa như sau
Chúng ta có thể truy cập các hằng số giao diện bằng cách sử dụng tên giao diện hoặc bằng cách sử dụng tên của lớp và cũng là tên của thể hiện của lớp thực hiện giao diện
Ví dụ: nếu chúng tôi xác định MAX_LENGTH cho giao diện Người dùng trong ví dụ đầu tiên của chúng tôi, thì hằng số này có thể được truy cập bằng cách sử dụng một số cách có thể sau đây
Trong chương trình trên, “traits” đã được sử dụng cùng với lớp cha. Có “lớp” tên là “Geeks” chứa hàm sayhello[] và một “đặc điểm” tên là “forGeeks” chứa hàm geeksforgeeks[] và có một lớp con tên là “Sample” và chúng ta đang tạo đối tượng của lớp này có tên là “
Đặc điểm [Sử dụng nhiều đặc điểm]. Nhiều Đặc điểm có thể được chèn vào một lớp bằng cách liệt kê chúng trong câu lệnh sử dụng, được phân tách bằng dấu phẩy.
Cú pháp.
class child_class_name { use trait_name; ... ... child_class functions }
Ví dụ
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
89Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90I am in interface C I am in interface B I am in inherited class2
class child_class_name { use trait_name; ... ... child_class functions }6
Hello Geeks GeeksforGeeks2
Hello Geeks GeeksforGeeks3
Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks6
Hello Geeks GeeksforGeeks7
Hello Geeks GeeksforGeeks8
Hello Geeks GeeksforGeeks9
class child_class_name { use trait_name; ... ... child_class functions }0
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
74class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
898class child_class_name { use trait_name; ... ... child_class functions }6
class child_class_name { use trait_name; ... ... child_class functions }7
Hello Geeks GeeksforGeeks3
Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks1
Hello Geeks GeeksforGeeks7
Hello Geeks GeeksforGeeks8
Hello Geeks GeeksforGeeks4
class child_class_name { use trait_name; ... ... child_class functions }0
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
74class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks1
Hello Geeks GeeksforGeeks05
Hello Geeks GeeksforGeeks3
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
75 Hello Geeks GeeksforGeeks08
Hello Geeks GeeksforGeeks3
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
75 Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
76Hello Geeks GeeksforGeeks3
Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
40
41Hello Geeks GeeksforGeeks8
43class child_class_name { use trait_name; ... ... child_class functions }0
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
74
46class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90
49 class child_class_name { use trait_name; ... ... child_class functions }30____531
class child_class_name { use trait_name; ... ... child_class functions }32
_______449____534
49class child_class_name { use trait_name; ... ... child_class functions }36
49____538class child_class_name { use trait_name; ... ... child_class functions }39
đầu ra. ________số 8
Trong chương trình trên, "traits" đã được sử dụng. Có hai đặc điểm có tên là “Geeks” chứa hàm sayhello[] và “forGeeks” chứa hàm geeksforgeeks[] tương ứng và có một lớp con “Sample” và chúng tôi đang tạo đối tượng của lớp này có tên là “test” và sử dụng nó
Giao diện [Sử dụng Lớp cùng với Giao diện].
Cú pháp.
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
7Ví dụ
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
89Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks1
Hello Geeks GeeksforGeeks38
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
74Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks42
Hello Geeks GeeksforGeeks43
Hello Geeks GeeksforGeeks8
Hello Geeks GeeksforGeeks45
class child_class_name { use trait_name; ... ... child_class functions }0
Hello Geeks GeeksforGeeks7
class child_class_name { use trait_name; ... ... child_class functions }2
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks51
Hello Geeks GeeksforGeeks52
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
74Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks56
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks1
Hello Geeks GeeksforGeeks60____172
Hello Geeks GeeksforGeeks62
Hello Geeks GeeksforGeeks63
Hello Geeks GeeksforGeeks52
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks43
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks68
Hello Geeks GeeksforGeeks69
Hello Geeks GeeksforGeeks8
Hello Geeks GeeksforGeeks71
class child_class_name { use trait_name; ... ... child_class functions }0
Hello Geeks GeeksforGeeks43
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks43
Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks79
Hello Geeks GeeksforGeeks43
Hello Geeks GeeksforGeeks8
Hello Geeks GeeksforGeeks82
class child_class_name { use trait_name; ... ... child_class functions }0
Hello Geeks GeeksforGeeks43
class child_class_name { use trait_name; ... ... child_class functions }2
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks88
class child_class_name { use trait_name; ... ... child_class functions }30____531
Hello Geeks GeeksforGeeks91
Hello Geeks GeeksforGeeks88
Hello Geeks GeeksforGeeks93
Hello Geeks GeeksforGeeks88
Hello Geeks GeeksforGeeks95
Hello Geeks GeeksforGeeks88
Hello Geeks GeeksforGeeks97
class child_class_name { use trait_name; ... ... child_class functions }39
đầu ra.
4Trong chương trình trên, Giao diện “B” đã được sử dụng cùng với lớp “A” để thực hiện đa kế thừa. Điểm quan trọng cần nhớ là, nó không thể định nghĩa hàm bên trong giao diện, nó phải được định nghĩa bên trong lớp con “Multiple”. Chúng ta đang gọi tất cả các hàm bằng cách sử dụng đối tượng lớp con [Nhiều] có tên là “geeks”
Giao diện [Sử dụng nhiều giao diện]
cú pháp
class child_class_name { use trait_name; ... ... child_class functions }3
Ví dụ
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
89Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks51
class child_class_name { use trait_name; ... ... child_class functions }02
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
74Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
class child_class_name { use trait_name; ... ... child_class functions }06
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks51
Hello Geeks GeeksforGeeks52
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
74Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks56
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks1
Hello Geeks GeeksforGeeks60____863
class child_class_name { use trait_name; ... ... child_class functions }20
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90_______843____523
Hello Geeks GeeksforGeeks43
Hello Geeks GeeksforGeeks5
Hello Geeks GeeksforGeeks68
Hello Geeks GeeksforGeeks69
Hello Geeks GeeksforGeeks8
class child_class_name { use trait_name; ... ... child_class functions }29
class child_class_name { use trait_name; ... ... child_class functions }0
Hello Geeks GeeksforGeeks43
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90_______843____535
Hello Geeks GeeksforGeeks43
Hello Geeks GeeksforGeeks5
class child_class_name { use trait_name; ... ... child_class functions }38
Hello Geeks GeeksforGeeks69
Hello Geeks GeeksforGeeks8
class child_class_name { use trait_name; ... ... child_class functions }41
class child_class_name { use trait_name; ... ... child_class functions }0
Hello Geeks GeeksforGeeks43
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks43
Hello Geeks GeeksforGeeks4
Hello Geeks GeeksforGeeks5
class child_class_name { use trait_name; ... ... child_class functions }49
_______843____551
Hello Geeks GeeksforGeeks69
Hello Geeks GeeksforGeeks8
Hello Geeks GeeksforGeeks82
class child_class_name { use trait_name; ... ... child_class functions }0
Hello Geeks GeeksforGeeks43
class child_class_name { use trait_name; ... ... child_class functions }2
class child_class_name { use trait_name; ... ... child_class functions }2
Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods [UserInterface::getContent] in ...
90Hello Geeks GeeksforGeeks88
class child_class_name { use trait_name; ... ... child_class functions }30____531
Hello Geeks GeeksforGeeks91
_______888____565
Hello Geeks GeeksforGeeks88
Hello Geeks GeeksforGeeks95
Hello Geeks GeeksforGeeks88
Hello Geeks GeeksforGeeks97
class child_class_name { use trait_name; ... ... child_class functions }39
đầu ra.
I am in interface C I am in interface B I am in inherited class
Trong chương trình trên, nhiều giao diện đã được sử dụng để thực hiện đa kế thừa. Trong ví dụ trên, có hai giao diện có tên là “B” và “C” đang đóng vai trò của các lớp cơ sở và có một lớp con có tên là “Multiple” và chúng tôi đang gọi tất cả các hàm bằng cách sử dụng đối tượng của nó có tên là “geeks”
PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này