PHP code example of laborra / php-ioc

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

    

laborra / php-ioc example snippets



$config = [ 
    // Bean configuration as PHP Array
    //... 
];

$context = ContextFactory::buildFromPHPArray($config);

// Or

$context = ContextFactory::buildFromFile('path/to/configurationfile.php');



// config.php
return [
    'beans' => [
        'beanOne' => [
            'ClassOne',
            'properties' => [
                'valueProp' => 42,
            ],
        ],
        'beanTwo' => [
            'ClassTwo',
            'properties' => [
                'refOne' => '@beanOne',
            ],
        ],

        'beanThree' => [
            'ClassThree',
            'constructorArgs' => [
                @beanTwo,
                'Answer to life the universe and everything'
            ],
        ],
    ],

]

class ClassOne
{
    public $valueProp;
}

class ClassTwo
{
    /** @var ClassOne */
    public $refOne;
}

class ClassThree
{
    /** @var ClassTwo */
    private $refTwo;

    /** @var string */
    private $strProp

    public function __constructor (ClassTwo $refTwo, $strAtg)
    {
        $this->strProp = $strArg;
    }

    public function getIt ()
    {
        return $this->strProp.' = '.$this->refTwo->refOne->valueProp;
    }
}

// In application logic

$beanObj = $context->getBean('beanThree');
echo $beanObj->getIt(); // Answer to life the universe and everything = 42



class UserService
{
    /** @var IUserDAO
    public $userDAO;

    public function authenticate ($username, $password)
    {
        // Business logic to authenticate the user
        $user = $this->userDAO->getByUserName($username);

        if ($user == null) {
            throw new Exception("User $username not found");
        }

        if ($user->password != $password) {
            throw new Exception("Invalid credentials");
        }
        
        return $user;
    }
    
    public function register ($username, $password)
    {
        if ($username == "") {
            throw new UserException("Username field cannot be blank");
        }

        if ($password == "") {
            throw new UserException("Password field cannot be blank");
        }

        $user = $this->userDAO->create($username, $password);
        return $user;
    }
}

interface IUserDAO
{
    function getByUsername ($username);

    function create ($username, $password);
}

class User
{
    public $username;

    public $password;
}


$c = new ConfigurationBuilder();

$c->bean('beanOne', '\vendorName\library\ClassName')
    ->property('valueProp', 12)
    ->property('refProp', '@beanTwo')
    ->constructorArg('String value')
    ->constructorArg(42);

return $c->build();



class SpecificConfigurationBuilder extends ConfigurationBuilder
{
    public function beanOneRef()
    {
        return $this->prop('refProp', '@beanOneId');
    }
}


# Install Composer
curl -sS https://getcomposer.org/installer | php

# Add php-ioc as a dependency (use dev-master until a stable release is out)
php composer.phar