Hướng nội là một tính năng phổ biến trong bất kỳ ngôn ngữ lập trình nào cho phép các lớp đối tượng được lập trình viên thao túng. Bạn sẽ tìm thấy nội tâm đặc biệt hữu ích khi bạn không biết bạn cần thực hiện lớp hoặc phương pháp nào vào thời điểm thiết kế.
Hướng nội trong PHP cung cấp khả năng hữu ích để kiểm tra các lớp, giao diện, thuộc tính và phương thức. PHP cung cấp một chức năng số lượng lớn mà bạn có thể sử dụng để hoàn thành nhiệm vụ. Để giúp bạn hiểu nội tâm, tôi sẽ cung cấp một cái nhìn tổng quan ngắn gọn về một số lớp, phương pháp và chức năng của PHP bằng cách sử dụng các ví dụ trong PHP để làm nổi bật cách chúng được sử dụng.
Trong bài viết này, bạn sẽ thấy một vài ví dụ về cách sử dụng một số chức năng hướng nội PHP hữu ích nhất và một phần dành riêng cho API cung cấp chức năng tương tự như hướng nội, API phản ánh.
Chức năng nội tâm PHP
Trong ví dụ đầu tiên, tôi sẽ chứng minh một số ít các chức năng nội tâm của PHP. Bạn có thể sử dụng các chức năng này để trích xuất thông tin cơ bản về các lớp như tên của chúng, tên của lớp phụ huynh của họ, v.v.
- class_exists() - Kiểm tra xem một lớp đã được xác định
- get_class() - Trả về tên lớp của một đối tượng
- get_parent_class() - Trả về tên lớp của lớp cha mẹ đối tượng
- is_subclass_of() - Kiểm tra xem một đối tượng có lớp cha mẹ nhất định không
Dưới đây là mã PHP ví dụ chứa định nghĩa cho các lớp ____10 và The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.1 và thông tin đầu ra được trích xuất bởi các chức năng được liệt kê ở trên:
<?php class Introspection { public function description() { echo "I am a super class for the Child class.n"; } } class Child extends Introspection { public function description() { echo "I'm " . get_class($this) , " class.n"; echo "I'm " . get_parent_class($this) , "'s child.n"; } } if (class_exists("Introspection")) { $introspection = new Introspection(); echo "The class name is: " . get_class($introspection) . "n"; $introspection->description(); } if (class_exists("Child")) { $child = new Child(); $child->description(); if (is_subclass_of($child, "Introspection")) { echo "Yes, " . get_class($child) . " is a subclass of Introspection.n"; } else { echo "No, " . get_class($child) . " is not a subclass of Introspection.n"; } }Đầu ra của mã trên phải như sau:
The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.Bạn có thể xác định xem lớp đã cho có được xác định bằng phương thức class_exists() hay không, có đối số chuỗi đại diện cho tên của lớp mong muốn để kiểm tra và giá trị boolean tùy chọn có hay không gọi trình tải tự động trong quy trình.
Các phương thức get_class() và get_parent_class() trả về tên lớp của một đối tượng hoặc tên lớp cha mẹ của nó tương ứng. Cả hai lấy là đối số là một thể hiện đối tượng.
Các phương thức is_subclass_of() lấy một thể hiện đối tượng làm đối số đầu tiên và một chuỗi, đại diện cho tên lớp cha và trả về xem đối tượng có thuộc về một lớp là một lớp con của lớp được đưa ra làm đối số hay không.
Ở đây, một ví dụ thứ hai chứa định nghĩa cho giao diện The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.6 và lớp The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.7 và thông tin đầu ra được trích xuất bởi các chức năng được liệt kê ở trên. Như với ví dụ đầu tiên, tôi sẽ liệt kê các chức năng trước và sau đó hiển thị cho bạn một số mã.
- The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.8 - Trả về danh sách tất cả các lớp được khai báo
- The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.9 - Trả về tên của các phương thức lớp
- <?php interface ICurrencyConverter { public function convert($currency, $amount); } class GBPCurrencyConverter implements ICurrencyConverter { public $name = "GBPCurrencyConverter"; public $rates = array("USD" => 0.622846, "AUD" => 0.643478); protected $var1; private $var2; function __construct() {} function convert($currency, $amount) { return $rates[$currency] * $amount; } } if (interface_exists("ICurrencyConverter")) { echo "ICurrencyConverter interface exists.n"; } $classes = get_declared_classes(); echo "The following classes are available:n"; print_r($classes); if (in_array("GBPCurrencyConverter", $classes)) { print "GBPCurrencyConverter is declared.n"; $gbpConverter = new GBPCurrencyConverter(); $methods = get_class_methods($gbpConverter); echo "The following methods are available:n"; print_r($methods); $vars = get_class_vars("GBPCurrencyConverter"); echo "The following properties are available:n"; print_r($vars); echo "The method convert() exists for GBPCurrencyConverter: "; var_dump(method_exists($gbpConverter, "convert")); }0 - Trả về các thuộc tính mặc định của một lớp
- <?php interface ICurrencyConverter { public function convert($currency, $amount); } class GBPCurrencyConverter implements ICurrencyConverter { public $name = "GBPCurrencyConverter"; public $rates = array("USD" => 0.622846, "AUD" => 0.643478); protected $var1; private $var2; function __construct() {} function convert($currency, $amount) { return $rates[$currency] * $amount; } } if (interface_exists("ICurrencyConverter")) { echo "ICurrencyConverter interface exists.n"; } $classes = get_declared_classes(); echo "The following classes are available:n"; print_r($classes); if (in_array("GBPCurrencyConverter", $classes)) { print "GBPCurrencyConverter is declared.n"; $gbpConverter = new GBPCurrencyConverter(); $methods = get_class_methods($gbpConverter); echo "The following methods are available:n"; print_r($methods); $vars = get_class_vars("GBPCurrencyConverter"); echo "The following properties are available:n"; print_r($vars); echo "The method convert() exists for GBPCurrencyConverter: "; var_dump(method_exists($gbpConverter, "convert")); }1 - Kiểm tra xem giao diện có được xác định không
- <?php interface ICurrencyConverter { public function convert($currency, $amount); } class GBPCurrencyConverter implements ICurrencyConverter { public $name = "GBPCurrencyConverter"; public $rates = array("USD" => 0.622846, "AUD" => 0.643478); protected $var1; private $var2; function __construct() {} function convert($currency, $amount) { return $rates[$currency] * $amount; } } if (interface_exists("ICurrencyConverter")) { echo "ICurrencyConverter interface exists.n"; } $classes = get_declared_classes(); echo "The following classes are available:n"; print_r($classes); if (in_array("GBPCurrencyConverter", $classes)) { print "GBPCurrencyConverter is declared.n"; $gbpConverter = new GBPCurrencyConverter(); $methods = get_class_methods($gbpConverter); echo "The following methods are available:n"; print_r($methods); $vars = get_class_vars("GBPCurrencyConverter"); echo "The following properties are available:n"; print_r($vars); echo "The method convert() exists for GBPCurrencyConverter: "; var_dump(method_exists($gbpConverter, "convert")); }2 - Kiểm tra xem một đối tượng có định nghĩa phương thức không
Đầu ra của mã trên phải như sau:
ICurrencyConverter interface exists. The following classes are available: Array ( [0] => stdClass [1] => Exception [2] => ErrorException [3] => Closure [4] => DateTime [5] => DateTimeZone [6] => DateInterval [7] => DatePeriod ... [154] => GBPCurrencyConverter ) GBPCurrencyConverter is declared. The following methods are available: Array ( [0] => __construct [1] => convert ) The following properties are available: Array ( [name] => GBPCurrencyConverter [rates] => Array ( [USD] => 0.622846 [AUD] => 0.643478 ) ) The method convert() exists for GBPCurrencyConverter: bool(true)Bạn có thể xác định xem lớp đã cho có được xác định bằng phương thức class_exists() hay không, có đối số chuỗi đại diện cho tên của lớp mong muốn để kiểm tra và giá trị boolean tùy chọn có hay không gọi trình tải tự động trong quy trình.
Các phương thức get_class() và get_parent_class() trả về tên lớp của một đối tượng hoặc tên lớp cha mẹ của nó tương ứng. Cả hai lấy là đối số là một thể hiện đối tượng.
Các phương thức is_subclass_of() lấy một thể hiện đối tượng làm đối số đầu tiên và một chuỗi, đại diện cho tên lớp cha và trả về xem đối tượng có thuộc về một lớp là một lớp con của lớp được đưa ra làm đối số hay không.
Ở đây, một ví dụ thứ hai chứa định nghĩa cho giao diện The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.6 và lớp The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.7 và thông tin đầu ra được trích xuất bởi các chức năng được liệt kê ở trên. Như với ví dụ đầu tiên, tôi sẽ liệt kê các chức năng trước và sau đó hiển thị cho bạn một số mã.
The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.8 - Trả về danh sách tất cả các lớp được khai báo
The class name is: Introspection I am a super class for the Child class. I'm Child class. I'm Introspection's child. Yes, Child is a subclass of Introspection.9 - Trả về tên của các phương thức lớp
<?php interface ICurrencyConverter { public function convert($currency, $amount); } class GBPCurrencyConverter implements ICurrencyConverter { public $name = "GBPCurrencyConverter"; public $rates = array("USD" => 0.622846, "AUD" => 0.643478); protected $var1; private $var2; function __construct() {} function convert($currency, $amount) { return $rates[$currency] * $amount; } } if (interface_exists("ICurrencyConverter")) { echo "ICurrencyConverter interface exists.n"; } $classes = get_declared_classes(); echo "The following classes are available:n"; print_r($classes); if (in_array("GBPCurrencyConverter", $classes)) { print "GBPCurrencyConverter is declared.n"; $gbpConverter = new GBPCurrencyConverter(); $methods = get_class_methods($gbpConverter); echo "The following methods are available:n"; print_r($methods); $vars = get_class_vars("GBPCurrencyConverter"); echo "The following properties are available:n"; print_r($vars); echo "The method convert() exists for GBPCurrencyConverter: "; var_dump(method_exists($gbpConverter, "convert")); }0 - Trả về các thuộc tính mặc định của một lớp
<?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }Đầu ra của mã trên phải như sau:
Child is a subclass of Introspection. GBPCurrencyConverter implements ICurrencyConverter. The following methods are available: Array ( [0] => ReflectionMethod Object ( [name] => __construct [class] => GBPCurrencyConverter ) [1] => ReflectionMethod Object ( [name] => convert [class] => GBPCurrencyConverter ) ) The method convert() exists for GBPCurrencyConverter.Bạn có thể xác định xem lớp đã cho có được xác định bằng phương thức class_exists() hay không, có đối số chuỗi đại diện cho tên của lớp mong muốn để kiểm tra và giá trị boolean tùy chọn có hay không gọi trình tải tự động trong quy trình.
Phương pháp <?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }0 lấy một mảng các phương thức và có thể lấy một đối số tùy chọn là sự kết hợp bitmask của <?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }1, <?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }2, <?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }3, <?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }4, <?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }5 và <?php $child = new ReflectionClass("Child"); $parent = $child->getParentClass(); echo $child->getName() . " is a subclass of " . $parent->getName() . ".n"; $reflection = new ReflectionClass("GBPCurrencyConverter"); $interfaceNames = $reflection->getInterfaceNames(); if (in_array("ICurrencyConverter", $interfaceNames)) { echo "GBPCurrencyConverter implements ICurrencyConverter.n"; } $methods = $reflection->getMethods(); echo "The following methods are available:n"; print_r($methods); if ($reflection->hasMethod("convert")) { echo "The method convert() exists for GBPCurrencyConverter.n"; }6 để lọc danh sách dựa trên khả năng hiển thị.
API phản ánh cung cấp một triển khai tốt về sự phản ánh cho bạn khả năng tạo ra các ứng dụng phức tạp hơn, chẳng hạn như Apigen, mặc dù thảo luận thêm vượt quá mục tiêu này của bài viết này.
Bản tóm tắt
Trong bài viết này, bạn đã thấy cách sử dụng các chức năng nội tâm của PHP và API refection để có được thông tin về các lớp, giao diện, thuộc tính và phương thức. Mục đích của việc rút thông tin này là để hiểu rõ hơn về mã của bạn trong thời gian chạy và tạo các ứng dụng phức tạp.
Hình ảnh thông qua Fotolia