Hướng dẫn syntax of introspection in php - cú pháp của nội quan trong php

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()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
<?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"));
}

Đầ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()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

Nội tâm với ví dụ trong PHP là gì?

Hướng nội là khả năng của một chương trình để kiểm tra các đặc điểm của một đối tượng, chẳng hạn như tên của nó, lớp cha (nếu có), thuộc tính và phương thức. Với nội tâm, bạn có thể viết mã hoạt động trên bất kỳ lớp hoặc đối tượng nào.the ability of a program to examine an object's characteristics, such as its name, parent class (if any), properties, and methods. With introspection, you can write code that operates on any class or object.

Hướng nội trong lập trình là gì?

Code Introspection là khả năng kiểm tra các lớp, chức năng và từ khóa để biết chúng là gì, họ làm gì và những gì họ biết.Python cung cấp một số chức năng và tiện ích cho nội tâm mã.the ability to examine classes, functions and keywords to know what they are, what they do and what they know. Python provides several functions and utilities for code introspection.

Phương pháp nội tâm nào được sử dụng để kiểm tra xem một đối tượng có lớp cha được nhất định khô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.is_subclass_of() methods takes an object instance as its first argument and a string, representing the parent class name, and returns whether the object belongs to a class which is a subclass of the class given as argument.

Nội tâm trong công nghệ web là gì?

Hướng nội là việc sử dụng các API MÁY TÍNH HIỆU QUẢ và máy ảo (VMM) để hiển thị thông tin hệ thống cấp thấp từ tất cả các máy ảo (VMS) được lưu trữ bởi VMM.the use of hypervisor- and virtual machine monitor (VMM)-level APIs to expose low-level system information from all virtual machines (VMs) hosted by the VMM.