Home Backend Development PHP Tutorial Introduction to reflection of handwritten PHP API framework (3)

Introduction to reflection of handwritten PHP API framework (3)

Feb 10, 2023 am 10:41 AM
php api reflection

In the previous article "Handwritten PHP API Framework: Installation and Use of Composer (2)" we introduced the installation and use of Composer. In this article we will introduce the concept of reflection.

Reflection, intuitive understanding is to find the departure place and source based on the arrival place. Reflection refers to extending the analysis of PHP programs in the running state of PHP, exporting or proposing detailed information about classes, methods, properties, parameters, etc., including comments. This function of dynamically obtaining information and dynamically calling object methods is called reflection API.

Let’s take a look at a demo first:

<?php


function p($msg, $var)
{
    echo($msg.":".print_r($var, true)).PHP_EOL.PHP_EOL;
}


class Demo
{
    private $id;

    protected $name;

    public $skills = [];

    public function __construct($id, $name, $skills = [])
    {
        $this->id = $id;
        $this->name = $name;
        $this->skills = $skills;
    }

    public function getName()
    {
        return $this->name;
    }
    public function getSkill()
    {
        p(&#39;skill&#39;, $this->skills);
    }
}


$ref = new ReflectionClass(&#39;Demo&#39;);
if ($ref->isInstantiable()) {
    p(&#39;检查类是否可实例化isInstantiable&#39;, null);
}
$constructor = $ref->getConstructor();
p(&#39;获取构造函数getConstructor&#39;, $constructor);

$parameters = $constructor->getParameters();
foreach ($parameters as $param) {
    p(&#39;获取参数getParameters&#39;, $param);
}

if ($ref->hasProperty(&#39;name&#39;)) {
    $attr = $ref->getProperty(&#39;name&#39;);
    p(&#39;获取属性getProperty&#39;, $attr);
}

$attributes = $ref->getProperties();
foreach ($attributes as $row) {
    p(&#39;获取属性列表getProperties&#39;, $row->getName());
}

if ($ref->hasMethod(&#39;getSkill&#39;)) {
    $method = $ref->getMethod(&#39;getSkill&#39;);
    p(&#39;获取方法getMethod&#39;, $method);
}

$methods = $ref->getMethods();
foreach ($methods as $row) {
    p(&#39;获取方法列表getMethods&#39;, $row->getName());
}

$instance = $ref->newInstanceArgs([1, &#39;sai&#39;, [&#39;php&#39;, &#39;js&#39;]]);
p(&#39;newInstanceArgs&#39;, $instance);
Copy after login

Output:

➜  php git:(main) php reflect.php 

检查类是否可实例化isInstantiable:

获取构造函数getConstructor:ReflectionMethod Object
(
    [name] => __construct
    [class] => Demo
)


获取参数getParameters:ReflectionParameter Object
(
    [name] => id
)


获取参数getParameters:ReflectionParameter Object
(
    [name] => name
)


获取参数getParameters:ReflectionParameter Object
(
    [name] => skills
)


获取属性getProperty:ReflectionProperty Object
(
    [name] => name
    [class] => Demo
)


获取属性列表getProperties:id

获取属性列表getProperties:name

获取属性列表getProperties:skills

获取方法getMethod:ReflectionMethod Object
(
    [name] => getSkill
    [class] => Demo
)


获取方法列表getMethods:__construct

获取方法列表getMethods:getName

获取方法列表getMethods:getSkill

newInstanceArgs:Demo Object
(
    [id:Demo:private] => 1
    [name:protected] => sai
    [skills] => Array
        (
            [0] => php
            [1] => js
        )

)
Copy after login

The ReflectionClass class is used in the demo. Of course, the ReflectionClass class is not limited to these methods.

More methods

The ReflectionClass class has more methods:

##ReflectionClass::__constructInitialize ReflectionClass ClassReflectionClass::exportExport a classReflectionClass::getConstantGet the definition A constantReflectionClass::getConstantsGet a set of constantsReflectionClass::getConstructorGet the constructor of the classReflectionClass::getDefaultPropertiesGet the default propertiesReflectionClass::getDocCommentGet documentation commentsReflectionClass::getEndLineGet the line number of the last lineReflectionClass:: getExtensionGet the ReflectionExtension object of the extension based on the defined classReflectionClass::getExtensionNameGet the name of the extension where the defined class is locatedReflectionClass::getFileNameGet the file name of the definition classReflectionClass::getInterfaceNamesGet Interface nameReflectionClass::getInterfacesGet interfaceReflectionClass::getMethodGet the ReflectionMethod of a class method. ReflectionClass::getMethodsGet the array of methodsReflectionClass::getModifiersGet the class ModifiersReflectionClass::getNameGet the class nameReflectionClass::getNamespaceName Get the name of the namespaceReflectionClass::getParentClassGet the parent classReflectionClass::getPropertiesGet a set of propertiesReflectionClass::getPropertyGet the ReflectionProperty of a property of the classReflectionClass:: getReflectionConstantGets a ReflectionClassConstant for a class's constantReflectionClass::getReflectionConstantsGets class constantsReflectionClass::getShortNameGet the short nameReflectionClass::getStartLineGet the starting line numberReflectionClass::getStaticPropertiesGet static properties##ReflectionClass::getStaticPropertyValueReflectionClass::getTraitAliasesReflectionClass::getTraitNamesReflectionClass::getTraitsReflectionClass::hasConstantReflectionClass::hasMethodReflectionClass::hasPropertyReflectionClass::implementsInterfaceReflectionClass::inNamespaceReflectionClass::isAbstractReflectionClass::isAnonymousReflectionClass::isCloneable##ReflectionClass::isFinalChecks whether the class is declared finalReflectionClass::isInstanceCheck the instance of the classReflectionClass::isInstantiableCheck whether the class is instantiableReflectionClass::isInterfaceCheck whether the class is an interfaceReflectionClass::isInternalCheck whether the class is represented by The extension or core defines internally ReflectionClass::isIterableCheck whether this class is iterableReflectionClass::isIterateable Check whether it is iterable(iterateable)ReflectionClass::isSubclassOfCheck whether it is a subclassReflectionClass::isTraitReturns whether it is a traitReflectionClass::isUserDefinedChecks whether it is defined by the userReflectionClass::newInstanceCreates a new class instance from the specified parametersCreate a new class instance from the given parameters. Creates a new class instance without calling its constructorSet the value of a static propertyReturns the string representation of the ReflectionClass object.
Method Description
Get the value of static properties
Returns an array of trait aliases
Returns An array of the names of traits used by this class
Returns an array of traits used by this class
Check whether the constant has been defined
Check whether the method has been defined
Check whether the property has been defined
Implementation of the interface
Check if it is in the namespace
Check if the class is abstract Class (abstract)
Check whether the class is an anonymous class
Returns whether a class can be copied
##ReflectionClass::newInstanceArgs
ReflectionClass::newInstanceWithoutConstructor
ReflectionClass:: setStaticPropertyValue
ReflectionClass::__toString

In addition to the powerful ReflectionClass, there are also Reflection, ReflectionClassConstant, ReflectionMethod, ReflectionFunctionAbstract and so on. It is recommended to check the manual:

Practical Application of Reflection

  • ##Reflection can be used for document and file generation. You can use it to scan the classes in the file and generate description documents one by one;

  • Since reflection can detect the internal structure of the class, you can use it as a hook to implement the plug-in function;

  • can be used as a dynamic proxy to dynamically generate and instantiate some classes and execution methods when the class name is unknown or uncertain;

  • Dependency injection. For classes that inherit multiple times, we can explore the structure of the base class through multiple reflections, or use recursive reflection to instantiate all inherited classes. This is also the principle of PHP dependency injection.

Advantages of Reflection

  • Languages ​​that support reflection provide some runtime features that are difficult to implement in low-level languages .

  • can avoid hard coding to a certain extent and provide flexibility and versatility.

  • Can discover and modify the structure of the source code (such as code blocks, classes, methods, protocols, etc.) as a first-class object.

  • You can calculate symbolic syntax strings at runtime just like source code statements (similar to JavaScript's eval() function), and then convert strings matching class or function into a call or reference to a class or function.

  • A new language bytecode interpreter can be created to give programming constructs a new meaning or purpose.

Disadvantages of reflection

  • The learning cost is high. Reflection-oriented programming requires more advanced knowledge, including frameworks, relational mapping and object interaction, to utilize more general code execution

  • Also because the concept and syntax of reflection are relatively abstract, Excessive abuse of reflection technology will make it difficult for others to read the code, which is not conducive to cooperation and communication

  • Reflection improves the flexibility of the code while sacrificing a little bit of operating efficiency. There is a certain amount of consumption

  • Reflection will also destroy the encapsulation of the class and expose methods or properties that should not be exposed

In In daily development, we don’t actually use reflection much, so why bring it here? Firstly, we will use reflection later to implement the Ioc container. Secondly, reflection is also one of the core functions of PHP. It is very common in our popular frameworks, and it is necessary to understand it.

This section is relatively independent, we will use it in subsequent chapters.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Introduction to reflection of handwritten PHP API framework (3). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles