简体   繁体   中英

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)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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