Core\Reflect\MethodInfo::parameterCount
$methodInfo->parameterCount(): uintReturns how many parameters a method declares.
parameterCount() returns a uint. $this is not counted, so a method written as total(int $net, int $tax) returns 2. A method with no parameters returns 0.
Core\Reflect\ClassInfo::call needs at least this many arguments. With fewer, it throws a LogicError and the method does not run. Extra arguments are ignored.
The number is also correct for a method the compiler adds, such as the constructor of LogicError. For those methods parameters() returns an empty array, and parameterCount() is the way to know what the method takes.
The examples below count the parameters of each method, show the count for an error class, and run only the tasks that need no arguments.
Return value
Returns uint. The number of parameters, as a uint. $this is not counted. The number is also correct for a method the compiler adds, where parameters returns an empty array.
Examples
Count the parameters
<?nvs// Shows how many parameters each method of a class declares.
class Mailer{ public function send(string $to, string $subject, string $body): bool { return true; }
public function ping(): bool { return true; }}
// This prints `ping: 0` and `send: 3`.foreach (Core\Reflect::forObject(new Mailer())->methods() as Core\Reflect\MethodInfo $method) { echo $method->name(), ': ', $method->parameterCount(), "\n";}ping: 0send: 3The constructor of an error class
<?nvs// Shows that `parameterCount` also works for a method that the compiler adds.
var $info = Core\Reflect::forClass('LogicError');if ($info != null) { foreach ($info->methods() as Core\Reflect\MethodInfo $method) { // The constructor of `LogicError` has no parameter list in any source file. // `parameters` is empty, but `parameterCount` still prints `2`. echo $method->name(), ': ', $method->parameterCount(), ' parameters, ', Core\Arr::count($method->parameters()), " listed\n"; }}constructor: 2 parameters, 0 listedRun the tasks that need no arguments
<?nvs// Shows how a nightly job runs every task of a class that needs no arguments.
class NightlyTasks{ public function clearCache(): string { return 'cache cleared'; }
public function sendReport(string $email): string { return 'report sent to ' . $email; }
public function rotateLogs(): string { return 'logs rotated'; }}
var $tasks = new NightlyTasks();var $info = Core\Reflect::forObject($tasks);foreach ($info->methods() as Core\Reflect\MethodInfo $method) { // `sendReport` needs an email address, so the job skips it. if ($method->parameterCount() != 0) { echo 'skipped ', $method->name(), "\n"; continue; } echo $info->call($tasks, $method->name(), []) as string, "\n";}cache clearedlogs rotatedskipped sendReport