PHP code example of anned20 / strix

1. Go to this page and download the library: Download anned20/strix 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/ */

    

anned20 / strix example snippets




nned20\Strix\Container;
use anned20\Strix\Exception\AlreadyInContainerException;
use anned20\Strix\Exception\NotFoundException;

// Create new container
$container = new Container();

// Use the container for variables
$container->add('config', ['hello' => 'world']);

// Use the container for closures
$container->add('function', function () {
	return rand();
});

// Let's use the config
$hello = $container->get('config')['hello'];

// And the function
$rand = $container->get('function')();

// Factories can be made too!
$container->add('factory', function () {
	return new SomeClass();
});

// Just like services
$myService = new SomeClass();

$container->add('service', $myService);

// Whoops!
$container->add('config', ['foo' => 'bar']); // AlreadyInContainerException thrown

// Let's check before adding
if (!$container->has('config')) {
	$container->add('config', ['foo' => 'bar']);
}

// But I want to overwrite the old one! No problem!
if ($container->has('config')) {
	$container->delete('config');
}

$container->add('config', ['foo' => 'bar']);

// Whoops!
$bye = $container->get('bye'); // NotFoundException thrown