PHP code example of webiik / container

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

    

webiik / container example snippets


$container = new \Webiik\Container\Container();
$container->addService('\Webiik\Arr\Arr', function () {
    return new \Webiik\Arr\Arr();
});
$array = $container->get('\Webiik\Arr\Arr');

addService(string $name, callable $factory): void

$container->addService('\Webiik\Arr\Arr', function () {
    return new \Webiik\Arr\Arr();
});

$container->addService('Service', function ($container) {
    // $container - Container    
});

addServiceFactory(string $name, callable $factory): void

$container->addService('\Webiik\Arr\Arr', function () {
    return new \Webiik\Arr\Arr();
});

addParam(string $name, $val): void

$container->addParam('foo', 'bar');

addFunction(string $name, callable $function): void

$container->addFunction('myFn', function ($a, $b) {
    return $a * $b;
});

isIn(string $name): bool

$container->isIn('\Webiik\Arr\Arr');

get(string $name)

$array = $container->get('\Webiik\Arr\Arr');

   $container->addService('\Webiik\Arr\Arr', function () {
      return new \Webiik\Arr\Arr();   
   });
   
   
   public function __construct(\Webiik\Arr\Arr $array)
   {
       $this->array = $array;
   }
   

   $myClass = new MyClass(...$container->injectTo('MyClass'));
   

   $container->addService('wsArray', function () {
      return new \Webiik\Arr\Arr();   
   });
   

   use Webiik\Arr\Arr as wsArray;
   

   /**
   * @param wsArray $array
   */   
   public function __construct(wsArray $array)
   {
       $this->array = $array;
   }
   

   $myClass = new MyClass(...$container->injectTo('MyClass'));

### Inject Function or Parameter
1. Add parameter with any name:
   
   
   public function __construct($myFnName)
   {
       $myFnName(); // Hello
   }
   

   $myClass = new MyClass(...$container->injectTo('MyClass'));
   
   
   public function __construct(\Webiik\Container\Container $container)
   {
       $this->container = $container;
   }
   

   $myClass = new MyClass(...$container->injectTo('MyClass'));