简体   繁体   English

PHP中“ Reflection”和“ Variable variables”有什么区别

[英]What's the difference between 'Reflection' vs 'Variable variables' in PHP

it seems that these 2 features has something in common, say, invoke a method just using its name in string. 似乎这两个功能有一些共同点,例如,仅使用字符串中的名称来调用方法。 what's the difference? 有什么不同? is there anything reflection can do but Variable variables can't? 反射可以做些什么,但是可变变量不能做?

even more, what's the difference between these 2 features' implementations in the PHP execution engine(VM) 而且,这两个功能在PHP执行引擎(VM)中的实现之间有什么区别

They don't have anything to do with each other. 他们彼此之间没有任何关系。

Variable variables let you utilize a variable containing a string to access the contents of a different variable. 变量变量使您可以利用包含字符串的变量来访问其他变量的内容。 They are confusing and rarely used in my experience. 它们令人困惑,在我的经验中很少使用。

<?php
$first = 'Bob';
$last = 'Smith';

$t = 'first';
echo $t;
echo $$t;

$t = 'last';
echo $t;
echo $$t;

Reflection is a general term used to describe facilities in a language or library that let you discover the structure of a class. 反射是用于描述语言或库中的设施的通用术语,可让您发现类的结构。 This is often useful to framework or component developers who are trying to implement design patterns that work with user supplied classes or have highly generic functionality. 对于试图实现与用户提供的类一起使用或具有高度通用功能的设计模式的框架或组件开发人员,这通常很有用。 They're also very helpful if you are generating php code, or providing a tool that documents existing code. 如果您要生成php代码或提供记录现有代码的工具,它们也非常有帮助。

As you can see from the api documentation: http://php.net/manual/en/book.reflection.php this provides you ways to determine at runtime the properties and methods of a class. 从api文档中可以看到: http : //php.net/manual/zh/book.reflection.php,这为您提供了在运行时确定类的属性和方法的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM