I haven't updated it for a long time. I wrote a placeholder article: if the default parameter of a function is a constant, how can I get the constant name of this parameter? See the code :? Phpfunctionnew_blog ($ titleDEFAULT_TITLE) {blahblah} in the code above, how to obtain the default constant name corresponding to the parameter $ title of the new_blog Function
I haven't updated it for a long time. I wrote a placeholder article: if the default parameter of a function is a constant, how can I get the constant name of this parameter? See the code :? Phpfunction new_blog ($ title = DEFAULT_TITLE) {// blahblah} in the code above, how can I obtain the default constant name corresponding to the parameter $ title of the new_blog function?
I haven't updated it for a long time. I wrote a placeholder article: if the default parameter of a function is a constant, how can I get the constant name of this parameter? See the Code:
In the code above, how to obtain the default constant name corresponding to the $ title parameter of the new_blog function:DEFAULT_TITLE
. This is similar to a blog post I wrote about how to get a variable name.
This problem is basically not solved before PHP5.4.6, because the function definition is the information during compilation and cannot be obtained during PHP runtime. Of course, this cannot be implemented because it cannot be implemented when the official PHP version is used.
Similar requirements in PHP generally use PHP reflection extensions.
PHP Reflection)Reflection is a standard extension provided in PHP5 for obtaining or operating PHP internal information. Users may write less application code and use it in framework or platform-based systems.
For example, your framework needs to implement a plug-in mechanism, and you may need to use reflection to obtain metadata of classes or functions. Here is not to Reflection the use of too much introduction, For details, see the official documentation: http://cn.php.net/manual/en/book.reflection.php
New Function ReflectionParameter: getdefadefavalueconstantname ()However, before PHP5.4.6, Reflection did not implement this function. This requirement is actually from the PHPUnit author Sebastian Bergmann. Because this requirement is a missing in the Reflection module and does not belong to major function upgrades, it directly enters the current latest branch PHP-5.4. This feature is also available in the PHP-5.4.6. Https://github.com/php/php-src/blob/PHP-5.4.6/NEWS#L41
For the implementation code, refer to "outputs. However, you can refer to the test cases.
This submission adds two functions to the ReflectionParameter class:
- ReflectionParameter: isdefavaluvalueconstant () is used to determine whether this parameter of the function is a constant default parameter.
- ReflectionParameter: getDefaultValueConstantName () is used to obtain the parameter name of the default parameter of this constant.
GetParameters () as $ param) {if ($ param-> getName () = 'test1') {var_dump ($ param-> isDefaultValueConstant ());} if ($ param-> getName () = 'test2') {var_dump ($ param-> isdefavaluvalueconstant ();} if ($ param-> isdefavaluvalueavailable () & $ param-> isdefavaluvalueconstant () {var_dump ($ param-> getdefavaluvalueconstantname ();} class Foo2 {const bar = 'foo2: bar ';} class Foo {const bar = 'foo: bar'; public fu Nction baz ($ param1 = self: bar, $ param2 = Foo2: bar, $ param3 = CONST_TEST_1) {}}$ method = new ReflectionMethod ('foo ', 'baz'); $ params = $ method-> getParameters (); foreach ($ params as $ param) {if ($ param-> isdefavaluvalueconstant ()) {var_dump ($ param-> getdefavaluvalueconstantname () ;}}?> // Running result bool (false) bool (true) string (12) "CONST_TEST_1" string (9) "self: bar" string (9) "Foo2 :: bar "string (12)" CONST_TEST_1"
Original article address: how to get the constant name of the default parameter of the PHP function, thanks to the original author for sharing.