{"id":15029162,"url":"https://github.com/xp-framework/reflection","last_synced_at":"2026-02-14T12:08:09.843Z","repository":{"id":43378086,"uuid":"318897408","full_name":"xp-framework/reflection","owner":"xp-framework","description":"This library provides a replacement for the XP Framework's reflection API, using a concise and fluent API.","archived":false,"fork":false,"pushed_at":"2025-10-04T08:49:21.000Z","size":512,"stargazers_count":2,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-04T09:38:50.802Z","etag":null,"topics":["annotations","attributes","php7","php8","reflection","xp-framework"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xp-framework.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-12-05T21:58:34.000Z","updated_at":"2025-10-04T08:48:46.000Z","dependencies_parsed_at":"2023-12-02T10:24:35.289Z","dependency_job_id":"cfb4b6c8-ff0b-48c0-9f85-f9770884ed7c","html_url":"https://github.com/xp-framework/reflection","commit_stats":{"total_commits":313,"total_committers":2,"mean_commits":156.5,"dds":"0.0031948881789137795","last_synced_commit":"86781565167244f17cdda5f405b521c349fd4e20"},"previous_names":[],"tags_count":60,"template":false,"template_full_name":null,"purl":"pkg:github/xp-framework/reflection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-framework%2Freflection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-framework%2Freflection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-framework%2Freflection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-framework%2Freflection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xp-framework","download_url":"https://codeload.github.com/xp-framework/reflection/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-framework%2Freflection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29443468,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T10:51:12.367Z","status":"ssl_error","status_checked_at":"2026-02-14T10:50:52.088Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotations","attributes","php7","php8","reflection","xp-framework"],"created_at":"2024-09-24T20:09:51.582Z","updated_at":"2026-02-14T12:08:09.838Z","avatar_url":"https://github.com/xp-framework.png","language":"PHP","readme":"XP Reflection\n=============\n\n[![Build status on GitHub](https://github.com/xp-framework/reflection/workflows/Tests/badge.svg)](https://github.com/xp-framework/reflection/actions)\n[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)\n[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)\n[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)\n[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/xp-framework/reflection/version.svg)](https://packagist.org/packages/xp-framework/reflection)\n\nThis library provides a replacement for the XP Framework's reflection API.\n\nFeatures\n--------\n**Concise**: This library aims at reducing code noise of the form `if (hasX(...)) { getX() }` by simply returning NULL from its accessor methods. Null handling has improved with the introduction of the null-coalesce operator `??` in PHP 7 and the null-safe invocation operator `?-\u003e` in PHP 8 (and in [XP Compiler](https://github.com/xp-framework/compiler)).\n\n**PHP 7 \u0026 8**: This library handles PHP 8 attributes in both PHP 7 (*using the compiler's AST library*) and PHP 8 (*using its native reflection API*).\n\n**Subcommand**: This library provides an [RFC #0303 integration](https://github.com/xp-framework/rfc/issues/303) and offers a \"reflect\" subcommand for the new XP runners. See `xp help reflect` on how to use it.\n\nAPI\n---\nThe entry point class is the `lang.Reflection` class. It can be constructed by passing either objects, type literals (e.g. `Date::class`), fully qualified class names (e.g. `util.Date`) or `lang.XPClass` or PHP's `ReflectionClass` instances.\n\n```php\nuse lang\\Reflection;\nuse org\\example\\{Base, Inject, Fixture};\n\n$type= Reflection::type(Fixture::class);\n\n$type-\u003ename();                // org.example.Fixture\n$type-\u003edeclaredName();        // Fixture\n$type-\u003eliteral();             // Fixture::class\n$type-\u003emodifiers();           // Modifiers\u003cpublic\u003e\n$type-\u003ecomment();             // (api doc comment)\n$type-\u003eclass();               // lang.XPClass instance\n$type-\u003eclassLoader();         // lang.ClassLoader instance\n$type-\u003epackage();             // Package or NULL\n$type-\u003eparent();              // Type or NULL\n$type-\u003einterfaces();          // Type[]\n$type-\u003etraits();              // Type[]\n$type-\u003ekind();                // Kind::$INTERFACE, Kind::$TRAIT, Kind::$CLASS, Kind::$ENUM\n$type-\u003eis(Base::class);       // true\n\nif ($type-\u003einstantiable()) {\n  $instance= $type-\u003enewInstance('Testing');\n}\n\n$type-\u003eisInstance($instance); // true\n```\n\nAnnotations can be accessed by iterating over `annotations()` or by the shorthand method `annotation()`.\n\n```php\nforeach ($type-\u003eannotations() as $annotation) {\n  $annotation-\u003etype();            // Author::class\n  $annotation-\u003ename();            // 'author'\n  $annotation-\u003earguments();       // ['Test', test =\u003e true]\n  $annotation-\u003eargument(0);       // 'Test'\n  $annotation-\u003eargument('test');  // true\n  $annotation-\u003enewInstance();     // Author class instance\n}\n\n$type-\u003eannotation(Inject::class); // Annotation or NULL\n```\n\nThe constructor can be accessed via `constructor()`. Like members, it provides accessors for modifiers and annotations, as well as its declaring type.\n\n```php\n$type-\u003econstructor();                      // Constructor or NULL\n\nif ($constructor= $type-\u003econstructor()) {\n  $constructor-\u003ename();                    // '__construct'\n  $constructor-\u003ecompoundName();            // 'org.example.Fixture::__construct()'\n  $constructor-\u003emodifiers();               // Modifiers\u003cpublic\u003e\n  $constructor-\u003ecomment();                 // (api doc comment)\n  $constructor-\u003eannotations();             // Annotations\n  $constructor-\u003eannotation(Inject::class); // Annotation or NULL\n  $constructor-\u003edeclaredIn();              // Type\n  $constructor-\u003eparameters();              // Parameters\n  $constructor-\u003eparameter(0);              // Parameter or NULL\n  $constructor-\u003enewInstance([]);           // (instance of the type)\n}\n```\n\nType instantiation can be controlled by using `initializer()`. It accepts either closures or named references to instance methods.\n\n```php\n// Instantiates type without invoking a constructor\n// Any passed arguments are discarded silently\n$instance= $type-\u003einitializer(null)-\u003enewInstance();\n\n// Instantiates type by providing a constructor, regardless of whether one exists or not\n// Arguments are passed on to the initializer function, which has access to $this\n$instance= $type-\u003einitializer(function($name) { $this-\u003ename= $name; })-\u003enewInstance(['Test']);\n\n// Instantiates type by selecting an instance method as an initializer\n// The unserialize callback is invoked with ['name' =\u003e 'Test']\nif ($unserialize= $type-\u003einitializer('__unserialize')) {\n  $instance= $unserialize-\u003enewInstance([['name' =\u003e 'Test']]);\n}\n```\n\nAll members (constants, properties and methods) can be accessed by iterating or by a shorthand lookup by name. Members provide accessors for modifiers and annotations, as well as their declaring type.\n\n```php\n$type-\u003econstant('POWER');                  // Constant or NULL\n$type-\u003eproperty('value');                  // Property or NULL\n$type-\u003emethod('fixture');                  // Method or NULL\n\nforeach ($type-\u003econstants() as $name =\u003e $constant) {\n  $constant-\u003ename();                       // 'POWER'\n  $constant-\u003ecompoundName();               // 'org.example.Fixture::POWER'\n  $constant-\u003evalue();                      // 6100\n  $constant-\u003emodifiers();                  // Modifiers\u003cpublic\u003e\n  $constant-\u003ecomment();                    // (api doc comment)\n  $constant-\u003eannotations();                // Annotations\n  $constant-\u003eannotation(Inject::class);    // Annotation or NULL\n  $constant-\u003edeclaredIn();                 // Type\n}\n\nforeach ($type-\u003eproperties() as $name =\u003e $property) {\n  $property-\u003ename();                       // 'value'\n  $property-\u003ecompoundName();               // 'org.example.Fixture::$value'\n  $property-\u003emodifiers();                  // Modifiers\u003cpublic\u003e\n  $property-\u003ecomment();                    // (api doc comment)\n  $property-\u003eannotations();                // Annotations\n  $property-\u003eannotation(Inject::class);    // Annotation or NULL\n  $property-\u003edeclaredIn();                 // Type\n  $property-\u003econstraint();                 // Constraint\n  $property-\u003eget($instance);               // (property value)\n  $property-\u003eset($instance, $value);       // (value)\n}\n\nforeach ($type-\u003emethods() as $name =\u003e $method) {\n  $method-\u003ename();                         // 'fixture'\n  $method-\u003ecompoundName();                 // 'org.example.Fixture::fixture()'\n  $method-\u003ecomment();                      // (api doc comment)\n  $method-\u003emodifiers();                    // Modifiers\u003cpublic\u003e\n  $method-\u003eannotations();                  // Annotations\n  $method-\u003eannotation(Inject::class);      // Annotation or NULL\n  $method-\u003edeclaredIn();                   // Type\n  $method-\u003ereturns();                      // Constraint\n  $method-\u003eparameters();                   // Parameters\n  $method-\u003eparameter(0);                   // Parameter or NULL\n  $method-\u003eclosure($instance);             // Closure instance\n  $method-\u003einvoke($instance, []);          // (method return value)\n}\n```\n\nMethod and constructor parameters can be retrieved by iterating via `parameters()`, by offset `parameter($position)` or by name `parameter($name)`.\n\n```php\n$method-\u003eparameter(0);                     // Parameter or NULL\n$method-\u003eparameter('arg');                 // Parameter or NULL\n\n$parameters= $method-\u003eparameters();        // Parameters instance\n$parameters-\u003eat(0);                        // Parameter or NULL\n$parameters-\u003enamed('arg');                 // Parameter or NULL\n\n$args= ['test'];\nif ($parameters-\u003eaccept($args)) {\n  $method-\u003einvoke(null, $args);\n}\n\nforeach ($parameters as $name =\u003e $parameter) {\n  $parameter-\u003eposition();                  // 0\n  $parameter-\u003ename();                      // 'arg'\n  $parameter-\u003evariadic();                  // false\n  $parameter-\u003eoptional();                  // true\n  $parameter-\u003edefault();                   // (parameter default value)\n  $parameter-\u003econstraint();                // Constraint\n  $parameter-\u003eannotations();               // Annotations\n  $parameter-\u003eannotation(Inject::class)    // Annotation or NULL\n}\n```\n\nPackages can be reflected upon by passing namespace names to `Reflection::of()`.\n\n```php\nuse lang\\Reflection;\n\n$package= Reflection::of('org.example');\n\n$package-\u003ename();                          // org.example\n$package-\u003eliteral();                       // 'org\\example'\n$package-\u003etype('Fixture');                 // Type instance\n$package-\u003eimplementation($t, 'Fixture');   // Type instance (subclass of $t)\n$package-\u003etypes();                         // iterable with Type instances\n$package-\u003eparent()                         // Package or NULL\n$package-\u003echild('impl')                    // Child package \"org.example.impl\" or NULL\n$package-\u003echildren();                      // iterable with Package instances\n$package-\u003eclassLoaders();                  // iterable with lang.ClassLoader instances\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-framework%2Freflection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxp-framework%2Freflection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-framework%2Freflection/lists"}