Php có thể triển khai nhiều giao diện không?

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 ...
89

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
90

I am in interface C
I am in interface B
I am in inherited class
2

class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
6
Hello Geeks
GeeksforGeeks
2

Hello Geeks
GeeksforGeeks
3
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
6

Hello Geeks
GeeksforGeeks
7
Hello Geeks
GeeksforGeeks
8
Hello Geeks
GeeksforGeeks
9
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 ...
74
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
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

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
898

class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
6
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
7

Hello Geeks
GeeksforGeeks
3
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
1

Hello Geeks
GeeksforGeeks
7
Hello Geeks
GeeksforGeeks
8
Hello Geeks
GeeksforGeeks
4
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 ...
74
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
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

Hello Geeks
GeeksforGeeks
1
Hello Geeks
GeeksforGeeks
05

Hello Geeks
GeeksforGeeks
3
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
GeeksforGeeks
08

Hello Geeks
GeeksforGeeks
3
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 ...
76

Hello Geeks
GeeksforGeeks
3
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5

40


41
Hello Geeks
GeeksforGeeks
8

43
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 ...
74

46

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


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


49
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
36


49____538

class 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 ...
7

Ví dụ




Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
89

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
90

Hello Geeks
GeeksforGeeks
1
Hello Geeks
GeeksforGeeks
38

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
74
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
42

Hello Geeks
GeeksforGeeks
43
Hello Geeks
GeeksforGeeks
8
Hello Geeks
GeeksforGeeks
45
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
0

Hello Geeks
GeeksforGeeks
7
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 ...
90

Hello Geeks
GeeksforGeeks
51
Hello Geeks
GeeksforGeeks
52

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
74
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
56

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

Hello Geeks
GeeksforGeeks
1
Hello Geeks
GeeksforGeeks
60____172
Hello Geeks
GeeksforGeeks
62
Hello Geeks
GeeksforGeeks
63
Hello Geeks
GeeksforGeeks
52

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
90

Hello Geeks
GeeksforGeeks
43
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
68

Hello Geeks
GeeksforGeeks
69
Hello Geeks
GeeksforGeeks
8
Hello Geeks
GeeksforGeeks
71
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
0

Hello Geeks
GeeksforGeeks
43
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

Hello Geeks
GeeksforGeeks
43
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
79

Hello Geeks
GeeksforGeeks
43
Hello Geeks
GeeksforGeeks
8
Hello Geeks
GeeksforGeeks
82
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
0

Hello Geeks
GeeksforGeeks
43
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 ...
90

Hello Geeks
GeeksforGeeks
88
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
30____531
Hello Geeks
GeeksforGeeks
91

Hello Geeks
GeeksforGeeks
88
Hello Geeks
GeeksforGeeks
93

Hello Geeks
GeeksforGeeks
88
Hello Geeks
GeeksforGeeks
95

Hello Geeks
GeeksforGeeks
88
Hello Geeks
GeeksforGeeks
97

class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
39

đầu ra.


4

Trong 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 ...
89

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
90

Hello Geeks
GeeksforGeeks
51
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 ...
74
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
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 ...
90

Hello Geeks
GeeksforGeeks
51
Hello Geeks
GeeksforGeeks
52

Fatal error: Class WebApp contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (UserInterface::getContent) in ...
74
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
56

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

Hello Geeks
GeeksforGeeks
1
Hello Geeks
GeeksforGeeks
60____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
GeeksforGeeks
43
Hello Geeks
GeeksforGeeks
5
Hello Geeks
GeeksforGeeks
68

Hello Geeks
GeeksforGeeks
69
Hello Geeks
GeeksforGeeks
8
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
29
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
0

Hello Geeks
GeeksforGeeks
43
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
GeeksforGeeks
43
Hello Geeks
GeeksforGeeks
5
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
38

Hello Geeks
GeeksforGeeks
69
Hello Geeks
GeeksforGeeks
8
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
41
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
0

Hello Geeks
GeeksforGeeks
43
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

Hello Geeks
GeeksforGeeks
43
Hello Geeks
GeeksforGeeks
4
Hello Geeks
GeeksforGeeks
5
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
49

_______843____551

Hello Geeks
GeeksforGeeks
69
Hello Geeks
GeeksforGeeks
8
Hello Geeks
GeeksforGeeks
82
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
0

Hello Geeks
GeeksforGeeks
43
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 ...
90

Hello Geeks
GeeksforGeeks
88
class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}
30____531
Hello Geeks
GeeksforGeeks
91

_______888____565

Hello Geeks
GeeksforGeeks
88
Hello Geeks
GeeksforGeeks
95

Hello Geeks
GeeksforGeeks
88
Hello Geeks
GeeksforGeeks
97

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

Lớp PHP có thể triển khai bao nhiêu giao diện?

Bạn có thể sửa đổi bộ đếm var trong vòng lặp for để làm cho tập lệnh đó tạo ra nhiều giao diện hơn nữa sẽ được triển khai bởi lớp "Bar". Nó dễ dàng hoạt động với lên đến 9999 giao diện (và rõ ràng là nhiều hơn nữa) như bạn có thể thấy từ đầu ra của dòng mã cuối cùng (print_r) khi thực thi tập lệnh đó.

Bạn có thể triển khai nhiều giao diện không?

Không cho phép đa kế thừa ( mở rộng ). Tuy nhiên, các giao diện không phải là các lớp và một lớp có thể triển khai nhiều hơn một giao diện . Các interface gốc được khai báo trong danh sách được phân tách bằng dấu phẩy, sau từ khóa implements.

Làm cách nào để triển khai hai giao diện trong một lớp trong PHP?

Một lớp có thể triển khai nhiều giao diện được phân tách bằng dấu phẩy trong phần khai báo (Sau từ khóa implements) . Nếu bạn làm điều đó, bạn phải khai báo tất cả các phương thức trừu tượng trong lớp.

Bạn có thể triển khai bao nhiêu giao diện?

Một lớp có thể triển khai bất kỳ số lượng giao diện nào , cho phép một lớp có nhiều hành vi.