PHP code example of pchouse / php-di

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

    

pchouse / php-di example snippets


    return [

        new Bind(
            Scope::TRANSIENT,
            IDependency::class,
            Dependency::class
        ),
    
        new Bind(
            Scope::TRANSIENT,
            IDependencyWiredOne::class,
            DependencyWiredOne::class
        ),
    
        new Bind(
            Scope::TRANSIENT,
            IDependencyWiredTwo::class,
            DependencyWiredTwo::class
        ),
    
        new Bind(
            Scope::SINGLETON,
            IDependencyWiredSingleton::class,
            DependencyWiredSingleton::class
        ),
    
        new Bind(
            Scope::PROVIDES,
            DependencyProvides::class,
            function () {
                $provides = new DependencyProvides();
                $provides->setRandom(
                    (new Randomizer())->getInt(9, 99999)
                );
                return $provides;
            }
        ),
    
        new Bind(
            Scope::SINGLETON,
            DependencyProvidesSingleton::class,
            function () {
                $provides = new DependencyProvidesSingleton();
                $provides->setRandom(
                    (new Randomizer())->getInt(9, 99999)
                );
                return $provides;
            }
        ),
    ];
    
    return [
        new Bind(Scope::ROUTE, IDependencyController::class, DependencyController::class),
    ];
 

    
    Container::$bindsFilePath  = "/path/to/Binds.php";
    Container::$routesFilePath = "/path/to/Routes.php";
 
    
    class Dependency implements IDIEvents, IDependency
    {
    
        private bool $afterInstanceCreatedInit = false;
    
        private bool $beforeReturnInstanceInit = false;
    
        #[Inject]
        private IDependencyWiredOne $dependencyWiredOne;
    
        private int $rand = 0;
    
        #[Inject]
        public function __construct(private IDependencyWiredTwo $dependencyWiredTwo)
        {
        }
        
        public function getDependencyWiredOne(): IDependencyWiredOne
        {
            return $this->dependencyWiredOne;
        }
        
        public function setDependencyWiredOne(IDependencyWiredOne $dependencyWiredOne): void
        {
            $this->dependencyWiredOne = $dependencyWiredOne;
        }
        
        public function getDependencyWiredTwo(): IDependencyWiredTwo
        {
            return $this->dependencyWiredTwo;
        }
        
        public function setDependencyWiredTwo(IDependencyWiredTwo $dependencyWiredTwo): void
        {
            $this->dependencyWiredTwo = $dependencyWiredTwo;
        }
    
        public function afterInstanceCreated(): void
        {
            $this->afterInstanceCreatedInit = true;
        }
    
        /**
         * @throws \Exception
         */
        public function beforeReturnInstance(): void
        {
            if (!$this->afterInstanceCreatedInit) {
                throw new \Exception(
                    "The AfterInstanceCreated did not run"
                );
            }
            $this->beforeReturnInstanceInit = true;
        }
        
        public function isAfterInstanceCreatedInit(): bool
        {
            return $this->afterInstanceCreatedInit;
        }
        
        public function isBeforeReturnInstanceInit(): bool
        {
            return $this->beforeReturnInstanceInit;
        }
        
        public function getRand(): int
        {
            return $this->rand;
        }
        
        public function setRand(int $rand):void
        {
            $this->rand = $rand;
        }
    }
 
    
    $instance = \PChouse\Di\Container::get(ICalssName);
    // OR
    $route = \PChouse\Di\Container::getRoute(IClassName);  
        
    
       
     Container::build(
            [
                new Bind(Scope::MOCK, IDependencyWiredSingleton::class, $mock)
            ]
     );

     $dependency = Container::get(IDependency::class);

 
    
    Scope::TRANSIENT // Allways a new instance
    Scope::SINGLETON // Create a singleton instance
    Scope::PROVIDES  // For manually created instaces
bash
composer