Explanation of the PHP reflection mechanism

Source: Internet
Author: User
PHP 5 has a complete reflection API that adds the ability to reverse engineer classes, interfaces, functions, methods, and extensions. In addition, the reflection API provides a way to remove document comments from functions, classes, and methods.

Use of reflection in the TP frame:

    • reflectionclass::__construct-constructs a Reflectionclass class

Public reflectionclass::__construct (mixed $argument)
    • Reflectionclass::newinstanceargs-creates a new class instance from the parameters given, which is passed to the constructor of the class.

public object Reflectionclass::newinstanceargs ([array $args])
    • /thinkphp/library/think/container.php

/** * Invoke reflection Execution Class instantiation support Dependency Injection * @access public * @param string $class class name        * @param array $vars parameter * @return Mixed */Public function Invokeclass ($class, $vars = []) {            try {$reflect = new Reflectionclass ($class);            $constructor = $reflect->getconstructor (); Injection $args = $constructor used to support dependencies?            $this->bindparams ($constructor, $vars): [];        Return $reflect->newinstanceargs ($args);        } catch (Reflectionexception $e) {throw new ClassNotFoundException (' Class NOT exists: '. $class, $class); }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.