Skip to content

Add lang.reflection.Invocable to reflect callable closures - #52

Merged
thekid merged 29 commits into
mainfrom
feature/closure
Nov 15, 2026
Merged

thekid merged 29 commits into
mainfrom
feature/closure

Conversation

@thekid

@thekid thekid commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Creating invocables

use lang\Reflection;

// Referring to a global function
$reflect= Reflection::invocable('strlen');

// Referring to a closure
$reflect= Reflection::invocable(fn($name) => "Hello {$name}");

// From a method
$type= Reflection::type('Test');
$reflect= $type->method('length')->invocable($type->newInstance()); // or NULL for static methods

Invocable API

use lang\reflection\Invocable;

$reflect= new Invocable(/** Handle */ fn(string $name): string => '@'.strtolower($name));
$reflect->anonymous();         // TRUE
$reflect->name();              // null for anonymous functions like above, "ns.name" otherwise
$reflect->literal();           // null for anonymous functions like above, "ns\\name" otherwise
$reflect->declaredName();      // null for anonymous functions like above, "name" otherwise
$reflect->package();           // lang.reflection.Package<global>
$reflect->comment();           // "Handle"
$reflect->annotations();       // lang.reflection.Annotations
$reflect->annotation($type);   // ?lang.reflection.Annotation
$reflect->parameters();        // lang.reflection.Parameters(size: 1)
$reflect->parameter(0);        // lang.reflection.Parameter(name: "name")
$reflect->parameter('name');   // -"-
$reflect->returns();           // lang.reflection.Constraint(type: 'string', present: true)
$reflect->invoke(['Test']);    // "@test"

Note: Annotations are only supported in PHP 8.0+!

@thekid

thekid commented Nov 9, 2026 •

Copy link
Copy Markdown
Member Author

Questions

  • Should this class be named Invocable instead? It matches the invoke() method and describes what the class reflects, whatever syntax created the value. Its only drawback is that it sounds a bit like an interface name.
  • Should we have $closure->invoke($bind, ['Test']) to match Routine::invoke()?

@thekid thekid added the enhancement New feature or request label Nov 9, 2026
@thekid thekid changed the title Add lang.reflection.Closure to reflect callable closures Add lang.reflection.Invocable to reflect callable closures Nov 14, 2026
@thekid

thekid commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

All dependant libraries' tests pass

@thekid
thekid merged commit 6cc5725 into main Nov 15, 2026
@thekid
thekid deleted the feature/closure branch November 15, 2026 11:33
@thekid

thekid commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant