Hướng dẫn example of introspection in php - ví dụ về xem xét nội tâm 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:

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
  •  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
  •  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
  •  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
 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

 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

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

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
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,
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,
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,
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,
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à
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

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

Trong điện toán, loại nội tâm là khả năng của một chương trình để kiểm tra loại hoặc thuộc tính của một đối tượng trong thời gian chạy.Một số ngôn ngữ lập trình sở hữu khả năng này.the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this capability.

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.

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?

Chức năng nội tâm PHP..
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 của một đối tượng ..
is_subClass_of () - Kiểm tra xem một đối tượng có lớp cha được nhất định không ..

Có bao nhiêu loại chức năng nội tâm trong SASS 11?

Có thể là số, chuỗi, màu, danh sách, bản đồ, bool, null, chức năng, argrist.number, string, color, list, map, bool, null, function, arglist.