PHP code example of hesam-mousavi / falcon-container

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

    

hesam-mousavi / falcon-container example snippets



~~~
- then create the container:

~~~php
$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();
~~~
## Singleton Service
If you want to use a service as a singleton:

~~~php
$container->singleton('test', Test::class);
// Or
$container->singleton(Test::class);
~~~

To execute the service in the first case:

~~~php
$container->get('test');
~~~

In the second case:

~~~php
$container->get(Test::class);
~~~

## Non-Singleton Service
If you `don't` want to use the service as a `singleton`, you need to `bind` it:

~~~php
$container->bind('test', Test::class);
// Or
$container->bind(Test::class);
~~~

Calling them is the same as with the singleton and both are accessible with `get` method:

~~~php
$container->get('test');
$container->get(Test::class);
~~~

## Using Closures
You can also use closures:

~~~php
$container->singleton('test', function ($container) {
    return $container->get(Test::class);
});
~~~

### Example
Suppose we have a class titled Test:

~~~php
namespace HesamMousavi\FalconContainer;

class Test {}
~~~

Result of Calling This Class:
~~~php
$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();

$container->singleton('test', \HesamMousavi\FalconContainer\Test::class);
//or
//$container->bind('test', \HesamMousavi\FalconContainer\Test::class);

dump($container->get('test'));
dump($container->get('test'));
~~~

Output in `Bind` Mode:
~~~
HesamMousavi\FalconContainer\Test {#6}
HesamMousavi\FalconContainer\Test {#13}
~~~
Output in `Singleton` Mode:
~~~
HesamMousavi\FalconContainer\Test {#6}
HesamMousavi\FalconContainer\Test {#6}
~~~

## Autowire Mode
If the class is not bound and called directly via the `get` method, it will be `automatically` resolved using reflection. This means that the `autowire` mode is `always active`, and if a bind(or singleton) is defined for a service, it uses it; otherwise, it resolves it automatically.

___
## Using `Service Provider` in Falcon Container
You can also use service providers. To do this, you need to create a class that extends `ServiceProvider`. You can have a directory called providers and place your providers there.

## Creating a ServiceProvider
~~~php
namespace HesamMousavi\FalconContainer\Providers;

use HesamMousavi\FalconContainer\FalconServiceProvider;
use HesamMousavi\FalconContainer\Test;

class TestServiceProvider extends FalconServiceProvider {
    public function register() {
        $this->container->singleton('test', Test::class);
    }

    public function boot() {}