PHP code example of zean / sim-di

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

    

zean / sim-di example snippets


    class Car
    {
        protected $name = '汽车';
     
        public function getName()
        {
            return $this->name;
        }
    }
    

    class Driver
    {
        protected $car;
     
        public function __construct(Car $car)
        {
            $this->car = $car;
        }
     
        public function drive()
        {
            return '驾驶' . $this->car->getName();
        }
    }
    

    $app = \SimDI\Container::getInstance();
    $driver = $app->get(Driver::class);
    echo $driver->drive();
    

abstract class Car
{
    protected $name = '汽车';
    
    public function getName()
    {
        return $this->name;
    }
}


interface Driveable
{
    public function run();
}

class Benz extends Car implements Driveable
{
    protected $name = '奔驰';
    
    public function run()
    {
        return $this->getName() . '启动了!';
    }
}

class Driver
{
    protected $car;
    
    public function __construct(Driveable $car)
    {
        $this->car = $car;
    }
    
    public function drive()
    {
        return '驾驶' . $this->car->run();
    }
}

$config = [
    Driveable::class => Benz::class,
];

$app = \SimDI\Container::getInstance($config);
$driver = $app->get(Driver::class);
echo $driver->drive();