PHP code example of stuartwakefield / platter

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

    

stuartwakefield / platter example snippets


"	"stuartwakefield/platter": "0.1.0"
}

$factory = new Platter(array(
	'dbuser' => 'admin',
	'dbpassword' => 'password',
	'dbname' => 'test',
	'dbhost' => '0.0.0.0',
	'connectionstring' => function ($container) {
		return "mysql:dbname={$container->get('dbname')};host={$container->get('dbhost')}";
	},
	'pdo' => function ($container) {
		return new PDO(
			$container->get('connectionstring'),
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	},
	'repository' => function ($container) {
		return new Repository($container->get('pdo'));
	}
));

$repository = $factory->get('repository');

$parent = new Platter(array(
	'dbuser' => 'xyz',
	'dbpassword' => 'abc'
));

$factory = new Platter(array(
	'DataSource' => function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	}
), $parent);

$ds = $factory->get('DataSource');

$parent = new Platter(array(
	'DataSource' => function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	}
));

$factory = new Platter(array(
	'dbuser' => 'xyz',
	'dbpassword' => 'abc'
), $parent);

$ds = $factory->get('DataSource'); // "Identifier 'dbuser' is not defined"

$parent = new Platter(array(
	'name' => 'Joe',
	'example' => function ($container) {
		return $container->get('name');
	}
));

$factory = new Platter(array(
	'name' => 'Michael'
), $parent);

echo $factory->get('name'); // "Michael"
echo $factory->get('example'); // "Joe"

$builder = new Platter\Builder;
$parent = $builder
	->register('dbuser', 'xyz')
	->register('dbpassword', 'abc')
	->build();

$builder = new Platter\Builder;
$factory = $builder
	->register('DataSource', function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	})
	->connect($parent)
	->build();

$factory->available(); // array('DataSource', 'dbpassword', 'dbuser');

$factory->defined(); // array('DataSource');

$builder
	->register('DataSource', new Platter\Definition\Singleton(function ($container) {
		return new DataSource(
			$container->get('dbuser'),
			$container->get('dbpassword')
		);
	}))
	->build();