English | 中文文档
super-kernel/support provides the make() function, which creates a new object that the caller owns and fills its constructor's dependencies from the application container.
composer require super-kernel/supportThe package has no requirement beyond PHP. To fill dependencies from a container, install super-kernel/context, which supplies the application's container; without it, make() constructs with the given arguments only.
make($class, $parameters), which returns a new instance on every call.- Constructor arguments by name or position, with the rest filled from the container by type, then from defaults and
null. - The same argument rules as
super-kernel/di, with any PSR-11 container thatsuper-kernel/contextholds. - Clear errors for a parameter that cannot be filled, an ambiguous type, and a by-reference parameter.
It does not:
- keep the created object in the container or return a container entry;
- apply
#[Provider]or#[FactoryProvider]mappings, property injection, or lifecycle hooks to the created object; - create a scope or a new copy of the dependencies: dependencies from the container stay shared.
A service the container provides. Save it as src/Report/Formatter.php:
<?php
declare(strict_types=1);
namespace Src\Report;
use function strtoupper;
final class Formatter
{
public function format(string $format, string $title): string
{
return strtoupper($format) . ': ' . $title;
}
}An object the caller owns, with a dependency and a setting. Save it as src/Report/Report.php:
<?php
declare(strict_types=1);
namespace Src\Report;
final readonly class Report
{
public function __construct(public Formatter $formatter, public string $format = 'text')
{
}
public function render(string $title): string
{
return $this->formatter->format($this->format, $title);
}
}In an application created from super-kernel/skeleton, create two reports. Save the script as bin/report.php:
<?php
declare(strict_types=1);
use Src\Report\Report;
use function SuperKernel\Support\make;
require dirname(__DIR__) . '/vendor/autoload.php';
$json = make(Report::class, ['format' => 'json']);
$text = make(Report::class);
echo $json->render('Sales'), PHP_EOL;
echo $text->render('Sales'), PHP_EOL;
var_dump($json === $text, $json->formatter === $text->formatter);Build the project and run the script:
skernel build
php bin/report.phpIt prints:
JSON: Sales
TEXT: Sales
bool(false)
bool(true)
| Name | Kind | Purpose |
|---|---|---|
SuperKernel\Support\make |
Helper | Creates a new instance of a class, filling its constructor from the given parameters and the application container. |
make()raisesInvalidArgumentExceptionwhen a parameter cannot be filled, such asUnable to resolve constructor parameter Src\Storage\Job::__construct($store) of type Src\Storage\Store., when more than one class or interface in a parameter's type has a container entry, and for a by-reference parameter.- A dependency is found only under the fully qualified name of a class or interface in the parameter's type. A container entry registered under another name, such as
mailer, is not used. - Exceptions of the container, of
super-kernel/context, and of the constructor pass through unchanged. - With
super-kernel/contextinstalled but no container set,make()lets the context create the default container from the build metadata, which needssuper-kernel/diandskernel build; set your own container first to use another one.
The developer documentation describes the argument rules, other containers, use without SuperKernel, and the errors with complete examples: SuperKernel Support.
Released under the MIT License. See LICENSE.