Configuration for resetting object state based on container definitions
When registering a PHP class in the container via a helper function or an attribute, you can specify a value that instructs the container on exactly how the object's state should be reset.
The container has the built-in capability to automatically generate a configuration for resetting object states based on the values provided for each registered PHP class; there is no need to manually define the \Kaspi\DiContainer\ObjectResetters class or its configuration.
The reset() method, provided by the \Kaspi\DiContainer\ObjectResetters class, will perform the state reset for every registered PHP class that has an associated state-reset value.
When registering a PHP class within configuration files, you must use the setResetter() method available via the following helper functions to specify the state-reset value:
When registering a PHP class using attributes, you must pass the state-reset value via the $resetter constructor parameter for the following attributes:
TIP
For applications using PHP 8.5 and higher, the $resetter constructor parameter for an attribute can be defined as a static anonymous function:
use Kaspi\DiContainer\Attributes\Autowire;
#[Autowire(
isSingleton: true,
resetter: static function (Foo $foo): void {
// implementation of an object-reset mechanism.
}
)]
final class Foo
{
//
}Example of configuring an object state reset
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
final class Foo
{
public function doReset(): void
{
// reset class state here
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
public function doResetPropertyFoo(): void
{
// reset the class property state here
}
public function doResetPropertyBar(): void
{
// reset the class property state here
}
}<?php
declare(strict_types=1);
// file: /app/src/Helpers/Resetters.php
namespace App\Helpers;
use App\Services\Baz;
final class Resetters
{
public static function resetObject(object $object): void
{
if ($object instanceof Baz) {
// reset the state of `App\Services\Baz` here
return;
}
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
final class Baz
{
}<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Services;
use Kaspi\DiContainer\Interfaces\ResetInterface;
final class Qux implements ResetInterface
{
public function reset(): void
{
// reset class state here
}
}You need to define how the state of objects in the container should be reset for the following PHP classes:
\App\Services\Foo: via the class methoddoReset().\App\Services\Bar: via an anonymous function that calls the methodsdoResetPropertyFoo()anddoResetPropertyBar(), which reset the state of two properties within that class.\App\Services\Baz: via the callable[App\Helpers\Resetters::class, 'resetObject'].\App\Services\Qux: via thereset()method, as defined by the\Kaspi\DiContainer\Interfaces\ResetInterfaceinterface.
NOTE
For the PHP class \App\Services\Qux, it is not necessary to explicitly declare a value for resetting the object's state; the class itself implements the \Kaspi\DiContainer\Interfaces\ResetInterface interface. When configuring the \Kaspi\DiContainer\ObjectResetters component, the container will automatically verify that the class implements the required interface and include it in the configuration for resetting object states.
Declaration in configuration files
Let’s examine the declaration of PHP classes in the services.php configuration file, where object state resetting is configured using the setResetter() method with various valid values.
<?php
declare(strict_types=1);
// file: /app/src/config/services.php
use App\Services\{Foo, Bar, Baz, Qux};
use App\Helpers\Resetters;
use function Kaspi\DiContainer\{diAutowire, diRuntime};
return static function (): Generator {
yield diAutowire(Foo::class, isSingleton: true)
->setResetter('doReset');
yield diAutowire(Bar::class, isSingleton: true)
->setResetter(static function (Bar $bar): void {
$bar->doResetPropertyFoo();
$bar->doResetPropertyBar();
});
yield diAutowire(Baz::class, isSingleton: true)
->setResetter([Resetters::class, 'resetObject']);
yield diRuntime(Qux::class, isSingleton: true);
};Declaration via attributes
Let’s examine the declaration of PHP classes using attributes, where the resetting of object state is configured by passing various valid values to the $resetter constructor parameter.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
#[Autowire(resetter: 'doReset')]
final class Foo
{
//…<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
use App\Helpers\Resetters;
#[Autowire(resetter: [Resetters::class, 'resetObject'])]
final class Baz
{
//…<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Services;
use Kaspi\DiContainer\Interfaces\ResetInterface;
use Kaspi\DiContainer\Attributes\DiRuntime;
#[DiRuntime]
final class Qux implements ResetInterface
{
//…For the PHP class \App\Services\Bar, resetting the object's state requires calling two class methods: doResetPropertyFoo() and doResetPropertyBar(). If using PHP 8.5 or higher, the $resetter constructor parameter for the attribute can be defined as a static anonymous function:
<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
#[Autowire(
resetter: static function (Bar $bar): void {
$bar->doResetPropertyFoo();
$bar->doResetPropertyBar();
},
)]
final class Bar
{
//…For PHP versions below 8.5, we can provide the $resetter constructor parameter as any callable type[1] for example, the function \App\do_reset_bar().
<?php
declare(strict_types=1);
// file: /app/functions/app_functions.php
namespace App;
// …
use App\Services\Bar;
if (!function_exists('App\do_reset_bar')) {
function do_reset_bar(Bar $bar): void {
$bar->doResetPropertyFoo();
$bar->doResetPropertyBar();
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
#[Autowire(resetter: 'App\do_reset_bar')]
final class Bar
{
//…The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎