Skip to content

Instantly share code, notes, and snippets.

@Majkl578
Last active September 10, 2018 12:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Majkl578/31ef8549b7e79d5878ac806acb3f3277 to your computer and use it in GitHub Desktop.
Save Majkl578/31ef8549b7e79d5878ac806acb3f3277 to your computer and use it in GitHub Desktop.
typed factory
<?php
declare(strict_Types=1);
namespace Example;
(function () : void {
$requestFactory = new class implements ServerRequestFactory {
public function __invoke() : ServerRequest
{
return SpecificServerRequest(123, 456);
}
};
(new RequestHandlerRunner($requestFactory))->run();
})();
<?php
declare(strict_Types=1);
namespace Example;
final class RequestHandlerRunner
{
/** ServerRequestFactory */
private $requestFactory;
public function __construct(ServerRequestFactory $requestFactory)
{
$this->requestFactory = $requestFactory;
}
public function run() : void
{
$request = ($this->requestFactory)();
// doi stuff with ServerRequest
}
}
<?php
declare(strict_Types=1);
namespace Example;
interface ServerRequestFactory
{
public function __invoke() : ServerRequest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment