Core\Reflect\ClassInfo::methods
$classInfo->methods(): array<Core\Reflect\MethodInfo>Lists every method of a class.
methods() returns an array with one Core\Reflect\MethodInfo for each method of the described class. The array is sorted by name. Each item gives the method's name with name(), whether it is public with isPublic(), and its number of parameters with parameterCount(). The list contains the methods the class inherits from its parent classes, and the constructor if the class has one.
Private methods are in the list too, with isPublic() returning false. The list only describes the methods. To run one, use call, which checks the visibility.
The examples below show the list for one class, the methods a subclass inherits, and a help text built from the public methods of a class.
Return value
Returns array<Core\Reflect\MethodInfo>. One Core\Reflect\MethodInfo for each declared method, sorted by name. A method that the calling code may not call is in the list, and its isPublic() returns false. Listing a method does not call it. Core\Reflect\ClassInfo::call checks whether this code may call it.
Changelog
| Version | Change |
|---|---|
0.0.6 | The server now builds the description of each class once, when it loads your program. Core\Reflect::forClass, Core\Reflect::forObject and Core\Reflect\EnumInfo::of return that description, so two calls for the same class return the same object. A list such as properties() or methods() is built once per request. |
Examples
List the methods of a class
<?nvs// Shows how to list every method of a class, with its visibility and its number of parameters.
class Invoice{ public float $total = 0.0;
public function addLine(string $item, float $price): void { $this->total = $this->total + $price; }
public function total(): float { return $this->total; }
private function roundCents(float $amount): float { return $amount; }}
var $info = Core\Reflect::forClass('Invoice');if ($info != null) { // `methods()` returns one `Core\Reflect\MethodInfo` for each method, sorted by name. // This prints three lines. The private method is in the list too. foreach ($info->methods() as Core\Reflect\MethodInfo $method) { string $visibility = $method->isPublic() ? 'public' : 'not public'; echo $method->name(), ': ', $visibility, ', ', $method->parameterCount(), " parameter(s)\n"; }}addLine: public, 2 parameter(s)roundCents: not public, 1 parameter(s)total: public, 0 parameter(s)Inherited methods are in the list
<?nvs// Shows that the list of methods also contains the methods a class inherits from its parent.
class Animal{ public function name(): string { return 'animal'; }
public function sleep(): string { return 'zzz'; }}
class Dog extends Animal{ public function bark(): string { return 'woof'; }}
class Methods{ public static function names(string $className): string { var $info = Core\Reflect::forClass($className); if ($info == null) { return 'no class ' . $className; } string $names = ''; foreach ($info->methods() as Core\Reflect\MethodInfo $method) { $names = $names . ' ' . $method->name(); } return $className . ':' . $names; }}
// `Animal` has two methods. `Dog` has its own `bark` and the two from `Animal`.echo Methods::names('Animal'), "\n";echo Methods::names('Dog'), "\n";Animal: name sleepDog: bark name sleepBuild the help text of a command line tool
<?nvs// Shows how a command line tool builds its help text from the public methods of its commands class.
class BackupCommands{ public function create(string $target): string { return 'backup of ' . $target; }
public function restore(string $file, string $target): string { return 'restore ' . $file . ' to ' . $target; }
public function list(): string { return 'no backups yet'; }
private function checksum(string $file): string { return $file; }}
class Help{ public static function text(string $className): string { var $info = Core\Reflect::forClass($className); if ($info == null) { return ''; } string $text = "Commands:\n"; foreach ($info->methods() as Core\Reflect\MethodInfo $method) { // A private method is a helper, so the help text does not show it. if (!$method->isPublic()) { continue; } // Each parameter is one argument the user must type. $text = $text . ' ' . $method->name() . ' (' . $method->parameterCount() . " argument(s))\n"; } return $text; }}
// This prints one line for each public method, sorted by name.echo Help::text('BackupCommands');Commands: create (1 argument(s)) list (0 argument(s)) restore (2 argument(s))