Home >Backend Development >PHP Problem >What does php provide to implement reflection

What does php provide to implement reflection

(*-*)浩
(*-*)浩Original
2019-09-17 13:06:051913browse

Even if a class member is defined as private, it can be accessed externally. You can access the members and methods of the class without creating an instance of the class.

What does php provide to implement reflection

PHP has added a reflection mechanism since version 5.0, which provides a set of powerful reflection APIs that allow you to access in the PHP running environment And use classes, methods, properties, parameters and comments, etc. Its functions are very powerful, often used in highly extensible PHP frameworks, automatically loading plug-ins, automatically generating documents, and can even be used to extend the PHP language. (Recommended learning: PHP programming from entry to proficiency)

Because it is a built-in oop extension of PHP, it is a feature of the language itself, so there is no need to add additional extensions Or configure it to use.

PHP reflection API will maintain corresponding reflection classes based on classes, methods, attributes, parameters, etc., and has provided corresponding calling APIs.

What does php provide to implement reflection

Access

Assuming that a class User is defined, we first need to create a reflection class instance of this class, and then based on this instance Can access properties or methods in User. Regardless of whether the member permission declaration defined in the class is public, it can be obtained.

<?php 
namespace Extend;

use ReflectionClass;
use Exception;

/**
 * 用户相关类
 * Class User
 * @package Extend
 */
class User{
    const ROLE = &#39;Students&#39;;
    public $username = &#39;&#39;;
    private $password = &#39;&#39;;

    public function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }

    /**
     * 获取用户名
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * 设置用户名
     * @param string $username
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

    /**
     * 获取密码
     * @return string
     */
    private function getPassword()
    {
        return $this->password;
    }

    /**
     * 设置密码
     * @param string $password
     */
    private function setPassowrd($password)
    {
        $this->password = $password;
    }
}

$class = new ReflectionClass(&#39;Extend\User&#39;);  // 将类名User作为参数,即可建立User类的反射类
$properties = $class->getProperties();  // 获取User类的所有属性,返回ReflectionProperty的数组
$property = $class->getProperty(&#39;password&#39;); // 获取User类的password属性ReflectionProperty
$methods = $class->getMethods();   // 获取User类的所有方法,返回ReflectionMethod数组
$method = $class->getMethod(&#39;getUsername&#39;);  // 获取User类的getUsername方法的ReflectionMethod
$constants = $class->getConstants();   // 获取所有常量,返回常量定义数组
$constant = $class->getConstant(&#39;ROLE&#39;);   // 获取ROLE常量
$namespace = $class->getNamespaceName();  // 获取类的命名空间
$comment_class = $class->getDocComment();  // 获取User类的注释文档,即定义在类之前的注释
$comment_method = $class->getMethod(&#39;getUsername&#39;)->getDocComment();  // 获取User类中getUsername方法的注释文档

Note: The class name passed when creating a reflection class must contain the complete namespace, even if the use keyword is used. Otherwise, an exception will be thrown if the class name cannot be found.

The above is the detailed content of What does php provide to implement reflection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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