PHP code example of phpolar / property-injector

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

    

phpolar / property-injector example snippets


class Example1
{
    /**
     * Will set this property with the
     * value in the DI container
     * that is registered with 'DEPENDENCY_ID'
     */
    #[Inject("DEPENDENCY_ID")]
    public string $property;
}

class Example2
{
    /**
     * Will set this property with the
     * value in the DI container
     * that is registered with the claass name
     * in the type hint
     */
    #[Inject]
    public SomeDependency $property;
}

class Example3
{
    /**
     * Will ignore protected properties.
     */
    #[Inject]
    protected SomeDependency $property;

    /**
     * Will ignore private properties.
     */
    #[Inject]
    private SomeDependency $property;
}

$injectee = new Example1();
(new PropertyInjector($psr11Container))->inject($injectee);

$injectee->property === $psr11Container->get("DEPENDENCY_ID"); // true