PHP code example of squidit / container-mason

1. Go to this page and download the library: Download squidit/container-mason library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

squidit / container-mason example snippets




$serviceClass1 = $container->get(Service::class);
$serviceClass2 = $container->get(Service::class);

if ($serviceClass1 === $serviceClass2) {
    echo 'We got the same instance';
} else {
    echo 'We got 2 different instances';
}



$serviceClass1 = $container->make(Service::class);
$serviceClass2 = $container->build(Service::class);
$serviceClass3 = $container->getNew(Service::class);



declare(strict_types=1);

namespace SquidIT\Container\Mason;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

interface ContainerMasonInterface extends ContainerInterface
{
    /**
     * Find an entry of the container by its identifier and returns it.
     * The returned entry will always be the same object instance/value
     *
     * @param string $id Identifier of the entry to look for.
     *
     * @throws ContainerExceptionInterface Error while retrieving the entry.
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
     *
     * @return mixed Entry.
     */
    public function get(string $id): mixed;

    /**
     * Find entry inside the container using identifier and always return a new instance.
     * All dependencies of the requested identifier will be resolved again to create new instances of all dependencies
     *
     * @param string $id Identifier of the entry to look for.
     *
     * @throws ContainerExceptionInterface Error while retrieving the entry.
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
     *
     * @return mixed Entry.
     */
    public function getNew(string $id): mixed;

    /**
     * Find entries inside the container using identifiers and always return an array with new instances.
     *
     * @param string ...$ids Identifiers of the entries to look for.
     *
     * @throws ContainerExceptionInterface Error while retrieving the entry.
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
     *
     * @return array<string, mixed> array key will be the id of the requested entry.
     */
    public function getNewMulti(string ...$ids): array;
    
    /**
     * Always returns a new container instance
     */
    public function getNewContainer(): ContainerInterface;
}
 bash
composer