withinboredom/nameof
最新稳定版本:v0.2.1
Composer 安装命令:
composer require withinboredom/nameof
包简介
Get the name of anything
README 文档
README
This is a library to get the name of a variable. Some example usages:
enum Foo { case Bar; case Baz; } $arr = []; $arr[nameof(Foo::Bar)] = 1; // resolves to 'Bar'
For compact:
$var = 1; $bar = 2; echo (compact(nameof($var), nameof($bar))); // ['var' => 1, 'bar' => 2]
Why?
This is useful when you want to use the name of a variable in a string, but want to retain easier refactoring. For example, normally when using compact you end up with a 'magic string' that references a variable name. However, this can be a problem if you want to refactor the variable name, as you have to change the string as well. This library allows you to use the nameof function to get the name of the variable, and then use that in the compact function. You can also use this for enum keys, and other places where you want to use the name of a variable.
Installation
composer require withinboredom/nameof
Example output
Here are some examples and their output, note that no error will be thrown if you pass it weird things because it isn't doing any parsing to make sure you are only passing sensible things.
echo nameof(1); // 1 echo nameof($myCallback(...)); // myCallback echo nameof($x->prop); // prop echo nameof($x->prop->fun(...)) // fun
统计信息
- 总下载量: 1.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-23