PHP code example of hypario / container

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

    

hypario / container example snippets


$builder = new Hypario\Builder();
$container = $builder->build();

class A {
    public function hello() {
        return "Hello World !";
    }
}

$builder = new Hypario\Builder();
$container = $builder->build();

$class = $container->get(A::class);

echo $class->hello(); // output : "Hello World !"

class A {
    
    private $name;

    public function __construct(string $name = "John") {
        $this->name = $name;
    }

    public function hello() {
        return "Hello $this->name !";
    }
}

$builder = new Hypario\Builder();
$container = $builder->build();

$class = $container->get(A::class);
echo $class->hello(); // output : "Hello John !"

class Address {

    public $address;

    public function __construct() {
        $this->address = 'France, Paris 6e'
    }
}

class Person {

    public $name;
    
    public $address;

    public function __construct(Address $address, string $name = 'John') {
        $this->name = $nom;
        $this->address = $address;
    }

    public function hello() {
        return "Hello $this->name, you live in $this->address";
    }

}

$builder = new Hypario\Builder();
$container = $builder->build();

$class = $container->get(Person::class);
echo $class->hello(); // output : "Hello John, you live in France, Paris 6e"

$builder = new Hypario\Builder();
$builder->addDefinitions(['foo' => 'bar']);
$container = $builder->build();

echo $container->get('foo'); // output : "bar"

interface testInterface{

    public function hello();

}

class test implements testInterface {

    public $string = "Hello I am the test class";

    public function hello(): string {
        return $this->string;
    }
}

class A {

    public $test;

    public function __construct(testInterface $test){
        $this->test = $test;
    }
}

$builder = new Hypario\Builder();
$builder->addDefinitions([
    testInterface::class => test::class
]);
$container = $builder->build();
$class = $container->get(A::class); 
echo $class->test->hello(); // output : "Hello I am the test class