6 kyu

Reflection in PHP #2 - Dissecting a function via ReflectionParameter

Description:

Reflection in PHP #2 - Dissecting a function via ReflectionParameter

About this Kata Series

This Kata Series explores a core API in PHP called Reflection which allows a developer to reverse-engineer classes, interfaces, functions, methods and extensions. It is assumed that the user undertaking this Kata Series is already familiar with both functional and object-oriented programming in PHP. A good indicator that you are ready for this Kata Series is if you are able to complete at least 6 out of 8 topics/Kata in my PHP Functions series without hesitation and are at least moderately familiar with all the topics covered in the first 7 Kata in my Object-Oriented PHP series. Certain Kata in this Series may also require slightly more advanced OOP knowledge such as the awareness of what an interface is, but if such extra knowledge is required, it will be mentioned under a "Prerequisites" subtitle.

Most Kata in this Series will consist of two main parts - a "Lesson" and a "Task". However, unlike either of "Object-Oriented PHP" and "PHP Functions", the "Lesson" will only teach the fundamental concepts required to understand the "Task" presented in each Kata. In most cases, you will be expected to look up on the official Reflection documentation in order to figure out the details of how to actually complete the Kata "Task" (e.g. which methods to call or even what class to use!). If you are still in doubt after referring to the official documentation, you are strongly encouraged to conduct your own research and use whatever resources are available to you on the Internet or otherwise.

Lesson

In the previous Lesson, we learned that we could extract basic information about the signature (i.e. declaration) of a function through the use of ReflectionFunction which is part of the Reflection API. For example, we could obtain information about whether said function is a closure and whether it has declared a return type. However, one main thing we have not touched upon is the function parameters themselves. What is the name of each parameter? Have they declared a type? Do they default to a given value if not provided? Enter ReflectionParameter! According to the official Documentation, this class also has a constructor which accepts 2 arguments (in order): the name of the function (or the closure itself) and the name of the parameter (without the $ symbol) as a string, so it should be technically possible to directly instantiate an instance of ReflectionParameter in our PHP script which will then allow us to study a particular parameter of a function in great detail. However, the more common (and better documented) method of studying a function's parameters is to first create a ReflectionFunction of said function and then calling on its getParameters() method to obtain an array of ReflectionParameters where each instance of ReflectionParameter corresponds to a parameter in the function signature. For example (Note: The <pre></pre> tags are just for styling for them to display properly in my browser):

function multiply($a, $b) {
  return $a * $b;
}

echo "<pre>";
var_dump((new ReflectionFunction('multiply'))->getParameters());
echo "</pre>";

/*

Outputs:

array(2) {
  [0]=>
  object(ReflectionParameter)#2 (1) {
    ["name"]=>
    string(1) "a"
  }
  [1]=>
  object(ReflectionParameter)#3 (1) {
    ["name"]=>
    string(1) "b"
  }
}

... in my browser

*/

Before we move on to our Task in this Kata, you may find the official documentation on ReflectionParameter useful as it contains an introduction to the class itself and contains links to each and every one of its properties/methods/constants.

Task

Write a function filter_parameters() which accepts exactly 1 argument $fn (the name of the function or the closure itself) and returns a string representation of a list containing the names of the parameters of said function that satisfies all of the conditions mentioned below:

  • It has declared a data type
  • Its declared data type is an integer
  • It has a default value
  • If not provided, it defaults to a value of 5

The returned string should be of the format "item1, item2, item3, ...", i.e. each parameter name in the list should be separated by the delimiter ", "

For example, for the following function:

function example($a, int $b, bool $c, int $d = 5, float $e = 5.00, int $f = 5, int $g = 15) { /* ... */ }

... your function should return "d, f". This test case has been included for you. You may assume that any function (or name thereof) passed into your function will have at least one parameter that satisfies all the conditions given.

Kata in this Series

  1. Reflection in PHP #1 - Introduction
  2. Reflection in PHP #2 - Dissecting a function via ReflectionParameter
  3. Reflection in PHP #3 - Using Reflection on Classes
  4. Reflection in PHP #4 - Puzzle Challenge [Assessment]

You May Also Like

Reflection
Object-oriented Programming
Fundamentals
Tutorials

Stats:

CreatedMar 16, 2017
PublishedMar 16, 2017
Warriors Trained363
Total Skips59
Total Code Submissions632
Total Times Completed195
PHP Completions195
Total Stars9
% of votes with a positive feedback rating96% of 54
Total "Very Satisfied" Votes50
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • donaldsebleung Avatar
  • ZED.CWT Avatar
Ad