> On Mar 14, 2022, at 3:20 PM, Lynn <kja...@gmail.com> wrote:
> 
> On Mon, Mar 14, 2022 at 8:04 PM Mike Schinkel <m...@newclarity.net 
> <mailto:m...@newclarity.net>> wrote:
> 
> Variable variables provide functionality[1] that if removed would force major 
> rewrites of PHP applications that were architected in a way that depends on 
> that functionality. 
> And while probably between 90% and 99% of the time when someone uses variable 
> variables there are better alternatives, variable variables make certain code 
> possible that would be impossible without them. 

> Do you have examples where this is the case? 

Most of these things cannot be done without the functionality provided by 
variable variables. However a few are examples could be handled differently but 
would require significant rewrite to fix without a new 
get_value_from_variable_name() function.  Either way, these are just from the 
first few pages of those 162.2k results on SourceGraph.com 
<http://sourcegroup.com/>:

https://github.com/microsoft/sql-server-samples/blob/master/samples/development-frameworks/laravel/vendor/jeremeamia/SuperClosure/src/SerializableClosure.php#L200-L208
 
<https://github.com/microsoft/sql-server-samples/blob/master/samples/development-frameworks/laravel/vendor/jeremeamia/SuperClosure/src/SerializableClosure.php#L200-L208>
https://github.com/mockery/mockery/blob/master/library/Mockery/Generator/StringManipulation/Pass/MethodDefinitionPass.php#L151
 
<https://github.com/mockery/mockery/blob/master/library/Mockery/Generator/StringManipulation/Pass/MethodDefinitionPass.php#L151>
https://github.com/vlucas/phpdotenv/blob/master/tests/Dotenv/DotenvTest.php#L251
 
<https://github.com/vlucas/phpdotenv/blob/master/tests/Dotenv/DotenvTest.php#L251>
https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc#L34-L38
 
<https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/PSR12/Tests/Classes/ClassInstantiationUnitTest.inc#L34-L384>
https://github.com/kanaka/mal/blob/master/impls/php/interop.php#L19 
<https://github.com/kanaka/mal/blob/master/impls/php/interop.php#L19>
https://github.com/moodle/moodle/blob/master/mod/data/tests/lib_test.php#L1293 
<https://github.com/moodle/moodle/blob/master/mod/data/tests/lib_test.php#L1293>
https://github.com/moodle/moodle/blob/master/mod/data/tests/generator_test.php?L105#L104
 
<https://github.com/moodle/moodle/blob/master/mod/data/tests/generator_test.php?L105#L104>
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/tests/Fixer/FunctionNotation/StaticLambdaFixerTest.php#L236
 
<https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/tests/Fixer/FunctionNotation/StaticLambdaFixerTest.php#L236>
https://github.com/phpipam/phpipam/blob/master/functions/PEAR/Net/Ping.php#L284 
<https://github.com/phpipam/phpipam/blob/master/functions/PEAR/Net/Ping.php#L284>
https://github.com/api-platform/core/blob/main/src/Core/Annotation/ApiProperty.php#L154-L159
 
<https://github.com/api-platform/core/blob/main/src/Core/Annotation/ApiProperty.php#L154-L159>
https://github.com/wp-cli/wp-cli/blob/master/php/WP_CLI/Runner.php#L1285-L1287 
<https://github.com/wp-cli/wp-cli/blob/master/php/WP_CLI/Runner.php#L1285-L1287>
https://github.com/phpmyadmin/phpmyadmin/blob/master/libraries/classes/Database/Qbe.php#L878-L879
 
<https://github.com/phpmyadmin/phpmyadmin/blob/master/libraries/classes/Database/Qbe.php#L878-L879>
https://github.com/PrestaShop/PrestaShop/blob/develop/classes/SpecificPrice.php#L474
 
<https://github.com/PrestaShop/PrestaShop/blob/develop/classes/SpecificPrice.php#L474>


> Using arrays is a compatible replacement as far as I know, and I highly doubt 
> these kinds of hacks are necessary.

"Necessary" is a judgement call.  If you never do meta-programming in PHP then 
these _techniques_ — calling them "hacks" is a bit incendiary — would not ever 
seem necessary. 

However if a developer does work on projects that by-requirement leverage 
meta-programming then variable variables are a tool in one's toolkit that one 
would be hobbled without.

An area where I have seen them required is in theming where the CMS or plugin 
vendor wants to make it easier on the themer by creating dynamically-named 
variables in scope and then including a PHP file intended to be used as a 
template, and the in other functions being able to access those 
dynamically-named variables.

Other areas are in developer tools written in PHP for PHP developers.

That said, are you objecting to the "variable variable" syntax, or more broadly 
to the ability to access the value of a variable given its name (as a new 
function could provide)?

-Mike

P.S. BTW, I agree that if a developer can avoid variable variables there would 
be doing themselves and future maintainers a big favor. I am only commenting 
because in rare cases variable variables provide a capability that you cannot 
achieved another way.  But if PHP provides that other way by way of a function 
then the only concern is BC breakage for those who have used the technique.





Reply via email to