PHP code example of decodelabs / slingshot

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

    

decodelabs / slingshot example snippets


use DecodeLabs\Slingshot;

$slingshot = new Slingshot(
    container: $container, // Psr\Container\ContainerInterface
    parameters: [
        'param1' => 'hello'
    ]
);

function test(
    string $param1,
    string $param2
) {
    return $param1 . ' '. $param2;
}

$output = $slingshot->invoke(test(...), [
    'param2' => 'world'
]); // hello world

use DecodeLabs\Harvest\Context as HarvestContext;

class Test {
    public function __construct(
        // Fetch or create a Harvest Context (example) from container
        HarvestContext $param1,
        string $param2
    ) {
        // ...
    }
}

$testObect = $slingshot->instantiate(Test::class, [
    'param2' => 'value'
]);

$object = new Test(...);
$slingshot->addType($object);

$slingshot->invoke(function(Test $test) {
    // ...
});