note 129851 added to function.get-class-methods

From: Date: Wed, 13 Nov 2024 02:38:46 +0000
Subject: note 129851 added to function.get-class-methods
Groups: php.notes 
Request: Send a blank email to php-notes+get-221648@lists.php.net to get a copy of this message
<?php /** * Retrieves detailed information about all methods in a given class. * * like get_class_methods() but can return more, like access level and attributes * * @param string $className The fully qualified name of the class to inspect. * @return array An associative array where each key is a method name, and the value is an array with the following structure: * ``` * [ * 'access' => (string) The access level of the method ('public', 'protected', or 'private'). * 'attributes' => (array) An array of attributes (annotations) associated with the method. Each attribute is represented as an array with: * [ * 'name' => (string) The name of the attribute. * 'arguments' => (array) The arguments passed to the attribute, if any. * ] * ] * ``` */ function get_class_methods_detailed(string $className): array { $reflectionClass = new \ReflectionClass($className); $result = []; foreach ($reflectionClass->getMethods() as $method) { $access = $method->isPublic() ? 'public' : ($method->isProtected() ? 'protected' : 'private'); $attributes = $method->getAttributes(); $result[$method->getName()] = [ 'access' => $access, 'attributes' => array_map(function ($attribute) { return [ 'name' => $attribute->getName(), 'arguments' => $attribute->getArguments(), ]; }, $attributes), ]; } return $result; } // Demo // CopyFile Class is from www.php.net/manual/en/language.attributes.overview.php $className = CopyFile::class; $detailedMethods = get_class_methods_detailed($className); echo "Detailed Methods for class " . $className . ":" . PHP_EOL; print_r($detailedMethods); // Will output: ?> Detailed Methods for class CopyFile: Array ( [fileExists] => Array ( [access] => public [attributes] => Array ( [0] => Array ( [name] => SetUp [arguments] => Array ( ) ) ) ) [targetDirectoryExists] => Array ( [access] => public [attributes] => Array ( [0] => Array ( [name] => SetUp [arguments] => Array ( ) ) ) ) [execute] => Array ( [access] => public [attributes] => Array ( ) ) ) ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 113.5.210.162) ---- Manual Page -- https://php.net/manual/en/function.get-class-methods.php Edit -- https://main.php.net/note/edit/129851 Del: integrated -- https://main.php.net/note/delete/129851/integrated Del: useless -- https://main.php.net/note/delete/129851/useless Del: bad code -- https://main.php.net/note/delete/129851/bad+code Del: spam -- https://main.php.net/note/delete/129851/spam Del: non-english -- https://main.php.net/note/delete/129851/non-english Del: in docs -- https://main.php.net/note/delete/129851/in+docs Del: other reasons-- https://main.php.net/note/delete/129851 Reject -- https://main.php.net/note/reject/129851 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221648) next »