Kiểm tra chức năng tồn tại trong lớp PHP

PHP có một chức năng tích hợp có tên là

";
}
else{

    echo "demofunction doesn't exists.
"; } if (function_exists('demofunction1')) { echo "demofunction1 exists
"; } else{ echo "demofunction1 doesn't exists.
"; } function demofunction1() { //Anything } ?>
3 để kiểm tra xem một chức năng có tồn tại hay không, có thể kiểm tra bất kỳ chức năng tích hợp hoặc được xác định nào. Hướng dẫn này trình bày cách sử dụng
";
}
else{

    echo "demofunction doesn't exists.
"; } if (function_exists('demofunction1')) { echo "demofunction1 exists
"; } else{ echo "demofunction1 doesn't exists.
"; } function demofunction1() { //Anything } ?>
3 trong PHP

Sử dụng "; } else{ echo "demofunction doesn't exists."; } if (function_exists('demofunction1')) { echo "demofunction1 exists"; } else{ echo "demofunction1 doesn't exists."; } function demofunction1() { //Anything } ?> 3 để kiểm tra tính khả dụng của chức năng tích hợp trong PHP

Giá trị trả về của

";
}
else{

    echo "demofunction doesn't exists.
"; } if (function_exists('demofunction1')) { echo "demofunction1 exists
"; } else{ echo "demofunction1 doesn't exists.
"; } function demofunction1() { //Anything } ?>
3 là boolean. Hãy kiểm tra các chức năng từ các thư viện khác nhau

Ví dụ

";
}
else
{
    echo "The function curl_close is not available.
"; } //Check a function from gettext library if (function_exists('gettext()')) { echo "The function gettext() is available.
"; } else { echo "The function gettext() is not available.
"; } //Check a function from ftp library if (function_exists('ftp_alloc')) { echo "The function ftp_alloc() is available.
"; } else { echo "The function ftp_alloc() is not available.
"; } //Check a function from GD library if (function_exists('imagecreatefromgif')) { echo "The function imagecreatefromgif is available.
"; } else { echo "The function imagecreatefromgif is not available.
"; } ?>

Mã kiểm tra các chức năng từ thư viện

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
0,
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
1,
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
2 và
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
3. Hai trong số các thư viện được bật và hai thư viện còn lại bị tắt

đầu ra

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.

Sử dụng "; } else{ echo "demofunction doesn't exists."; } if (function_exists('demofunction1')) { echo "demofunction1 exists"; } else{ echo "demofunction1 doesn't exists."; } function demofunction1() { //Anything } ?> 3 để kiểm tra các hàm do người dùng xác định trong PHP

";
}
else{

    echo "demofunction doesn't exists.
"; } if (function_exists('demofunction1')) { echo "demofunction1 exists
"; } else{ echo "demofunction1 doesn't exists.
"; } function demofunction1() { //Anything } ?>
3 có thể kiểm tra các chức năng được xác định trước hoặc sau khi kiểm tra

Ví dụ

";
}
else{

    echo "demofunction doesn't exists.
"; } if (function_exists('demofunction1')) { echo "demofunction1 exists
"; } else{ echo "demofunction1 doesn't exists.
"; } function demofunction1() { //Anything } ?>

Đoạn mã trên kiểm tra hai chức năng. Một được xác định trước và một sau khi kiểm tra

đầu ra

demofunction exists
demofunction1 exists

Kiểm tra tất cả các chức năng tồn tại trong môi trường trong PHP

PHP cũng có chức năng tích hợp để kiểm tra tất cả các chức năng trong môi trường

Ví dụ

________số 8

Đầu ra sẽ nhận được tất cả các chức năng tồn tại trong môi trường. Đầu ra là một mảng rất lớn, vì vậy chúng tôi đã thu nhỏ nó thành một vài phần tử

Tham số. Hàm này chấp nhận một tham số duy nhất $function_name. Đây là tên của chức năng mà chúng tôi muốn tìm kiếm trong danh sách chức năng được xác định. Đây là một tham số kiểu chuỗi

Giá trị trả về. Hàm này trả về một giá trị Boolean. Trong trường hợp một hàm có tên $function_name tồn tại, nó sẽ trả về TRUE, nếu không nó sẽ trả về FALSE. Hàm này cũng sẽ trả về FALSE cho các cấu trúc như “include_once”, “echo”, v.v.

Tóm lược. trong hướng dẫn này, bạn sẽ học cách sử dụng hàm PHP

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 để kiểm tra xem một lớp hoặc một đối tượng của một lớp có một phương thức được chỉ định hay không

Giới thiệu về hàm method_exists trong PHP

Hàm

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
5 nếu một đối tượng hoặc một lớp có một phương thức được chỉ định. Ngược lại, nó trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
6

Cú pháp của hàm

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 như sau

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
4

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 có hai tham số

  • class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

    Code language: PHP (php)
    9 là một đối tượng hoặc một lớp mà bạn muốn kiểm tra xem phương thức có tồn tại không
  • The function curl_close is available.
    The function gettext() is not available.
    The function ftp_alloc() is not available.
    The function imagecreatefromgif is available.
    
    70 là một chuỗi đại diện cho phương thức kiểm tra

Các ví dụ về hàm method_exists trong PHP

Hãy lấy một số ví dụ về cách sử dụng hàm

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3

1) Sử dụng hàm method_exists() của PHP để kiểm tra xem một lớp có phương thức hay không

Ví dụ sau sử dụng hàm

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 để kiểm tra xem một phương thức có tồn tại trong lớp
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
73 hay không

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)

Trong ví dụ này, câu lệnh sau trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
5 vì phương thức
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
75 tồn tại trong lớp
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
73

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
7

Tuy nhiên, câu lệnh sau trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
6 vì phương thức
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
78 không tồn tại trong lớp
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
73

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
9

2) Sử dụng hàm method_exists trong PHP để kiểm tra xem một đối tượng có phương thức hay không

Ví dụ sau tạo một đối tượng mới của

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
73 và sử dụng hàm

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 để kiểm tra đối tượng có một phương thức được chỉ định

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
2

Đối tượng

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
92 có phương thức
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
75, do đó, câu lệnh sau trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
5

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
6

Mặt khác, đối tượng $account không có phương thức

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
78. Do đó, câu lệnh sau trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
6

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
9

3) Sử dụng hàm method_exists để kiểm tra xem một đối tượng có phương thức tĩnh hay không

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 cũng trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
5 nếu một lớp có phương thức tĩnh. Ví dụ

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
2

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
73 có phương thức tĩnh
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
20, vì vậy câu lệnh sau trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
5

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
6

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
92 là một thể hiện của lớp
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
73 có phương thức tĩnh
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
20, biểu thức sau cũng trả về

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
5

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
1

Hàm method_exists của PHP trong các khung MVC

Phương thức

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 thường được sử dụng trong các khung Model-View-Controller (MVC) để kiểm tra xem một lớp trình điều khiển có một phương thức nhất định hay không trước khi gọi nó

Ví dụ: giả sử bạn có URI yêu cầu sau

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
0

URI này có ba phần. bài đăng, chỉnh sửa và 1

  • The function curl_close is available.
    The function gettext() is not available.
    The function ftp_alloc() is not available.
    The function imagecreatefromgif is available.
    
    27 ánh xạ tới lớp
    The function curl_close is available.
    The function gettext() is not available.
    The function ftp_alloc() is not available.
    The function imagecreatefromgif is available.
    
    28
  • The function curl_close is available.
    The function gettext() is not available.
    The function ftp_alloc() is not available.
    The function imagecreatefromgif is available.
    
    29 ánh xạ phương thức
    The function curl_close is available.
    The function gettext() is not available.
    The function ftp_alloc() is not available.
    The function imagecreatefromgif is available.
    
    29 của lớp
  • Số
    The function curl_close is available.
    The function gettext() is not available.
    The function ftp_alloc() is not available.
    The function imagecreatefromgif is available.
    
    61 là id bài đăng để chỉnh sửa

Lớp

The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
28 sẽ như sau

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
1

Và bạn dùng hàm

class BankAccount { public function transferTo(BankAccount $other, float $amount) { // more code } } $exists = method_exists(BankAccount::class, 'transferTo'); var_dump($exists); // bool(true) $exists = method_exists(BankAccount::class, 'deposit'); var_dump($exists); // bool(false)

Code language: PHP (php)
3 để kiểm tra phương thức edit có tồn tại trong đối tượng
The function curl_close is available.
The function gettext() is not available.
The function ftp_alloc() is not available.
The function imagecreatefromgif is available.
64 như thế này không

Làm cách nào để xác định hàm test() trong PHP?

php // Định nghĩa hàm test hàm(){ $greet = "Xin chào thế giới. "; echo $greet; } test(); // Kết quả đầu ra. Chào thế giới.

Chú thích nào được sử dụng để kiểm tra xem một phương thức có tồn tại trong lớp cha không?

Chúng tôi sử dụng chú thích @Override để đánh dấu một phương thức tồn tại trong lớp cha nhưng chúng tôi muốn ghi đè lên trong lớp con.

Có thể gọi được trong PHP không?

Hàm is_callable() kiểm tra xem nội dung của một biến có thể được gọi như một hàm hay không . Hàm này trả về true (1) nếu biến có thể gọi được, nếu không nó trả về false/không có gì.

Cách khai báo hàm trong PHP?

Để khai báo kiểu cho hàm trả về, thêm dấu hai chấm (. ) và nhập ngay trước dấu ngoặc nhọn ( { ) khi khai báo hàm .