PHP code example of cwm / hypo

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

    

cwm / hypo example snippets


class Crypt {
    public function crypt($password) {
        return md5($password);
    }
}

$container = new Container();
$container->register('Crypt');

$crypt = $container->resolve('Crypt');
echo $crypt->crypt('testing');

interface ICrypt {
    public function crypt($password);
}

class Crypt implements ICrypt {
    public function crypt($password) {
        return md5($password);
    }
}

$container = new Container();
$container
    ->register('Crypt')
    ->with('ICrypt');

$crypt = $container->resolve('ICrypt');

interface ICrypt {
    public function crypt($password);
}

interface IPasswordResetter {
    public function reset($user, $password);
}

class Crypt implements ICrypt, IPasswordResetter {
    public function crypt($password) {
        return md5($password);
    }

    public function reset($user, $password) {
        $user->setPassword($this->crypt($password));
    }
}

$container = new Container();
$container
    ->register('Crypt')
    ->with(array('ICrypt', 'IPasswordResetter'));

interface ICrypt {
    public function crypt($password);
}

interface IPasswordResetter {
    public function reset($user, $password);
}

class Crypt implements ICrypt, IPasswordResetter {
    public function crypt($password) {
        return md5($password);
    }

    public function reset(User $user, $password) {
        $user->setPassword($this->crypt($password));
    }
}

$container = new Container();
$container
    ->register('Crypt')
    ->withImplementedInterfaces();

$container = new Container();
$container
    ->register($instanceOfCrypt)
    ->withImplementedInterfaces();

interface ICrypt {
    public function crypt($password);
}

class BCrypt implements ICrypt {
    public function __construct($workfactor) {
        ...
    }
}

$container = new Container();
$container
    ->register('BCrypt')
    ->with('ICrypt')
    ->withParameters(array(
        'workfactor' => 10
    ));

$container = new Container();
$container
    ->register('Crypt')
    ->with('ICrypt')
    ->constructedBy(function($className) {
        return new $className(rand());
    });

interface ICrypt {
    public function crypt($password);
}

class BCrypt implements ICrypt {
    public function __construct($workfactor) {
        ...
    }
}

class MD5Crypt implements ICrypt {
    ...
}

$container = new Container();
$container
    ->register('BCrypt')
    ->with('ICrypt')
    ->withParameters(array(
        'workfactor' => 10
    ))
    ->withName('bcrypt');

$container->resolveName('bcrypt');

class UserService {
    public function __construct(ICrypt $crypt) {
        ...
    }
}

$container
    ->register('UserService')
    ->withParameters(array(
        'crypt' => new NamedDependency('bcrypt')
    ));

interface IUserRepo {
    ...
}

class UserRepo implements IUserRepo {
    ...
}

$container = new Container();
$container
    ->register('UserRepo')
    ->with('IUserRepo')
    ->asSingleton();