PHP code example of dastur / capsule

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

    

dastur / capsule example snippets


$capsule = new Capsule();

$capsule->bind(string $name, mixed $path, callable $value, boolean $singleton = false)

$capsule->bind('service', Service::class, function() {
  return new Service();
});

$capsule->singleton('service', Service::class, function() {
  return new Service();
});

$capsule->get(mixed $name)

$capsule->get('service') or $capsule->get(Service::class)

 $capsule->service or $capsule['service']
 

$capsule->make(mixed $path, array $parameters = [])


namespace Package\Service

class Service
{
  public $anotherService;
  public $string;
  
  public function __construct(AnotherService $anotherService, $string)
  {
    $this->anotherService = $anotherService;
    $this->string = $string;
  }
}

$service = $capsule->make(\Package\Service::class, ['string' => 'randomString')


namespace Package\Service

class Service
{
  public $router;
  public $logger;
  
  public function __construct(Router $router, Logger $logger)
  {
    $this->router = $router;
    $this->logger = $logger;
  }
}

$capsule->make(\Package\Service::class)

$capsule->bind('service', Service::class)

$capsule->bind('service', Service::class, function($capsule) {
  return $capsule->make(Service::class);
});

$capsule->bind('service', Service::class, ['string' => 'random'])

$capsule->destroy(mixed $name)

$capsule->destroy('service') or $capsule->destroy(Service::class)

unset($capsule->service) or unset($capsule['service'])