wilkques/container

v4.0.0 2024-09-25 05:37 UTC

This package is auto-updated.

Last update: 2024-10-25 05:58:30 UTC


README

Latest Stable Version License

How to use

composer require wilkques/container

require '<project path>/vendor/Container/src/helpers.php';
require '<project path>/vendor/Container/src/Container.php';

Method

  1. register

    container()->register(
    '<your class name>',
    new '<your class name>'
    );
    
    // or
    
    container()->register([
    	[
    		'<your class name1>',
    		new '<your class name1>'
    	],
    	[
    		'<your class name2>',
    		new '<your class name2>'
    	],
    
    	...
    ]);
  2. bind

    $abstract = new \Your\Class\Name;
    
    container()->bind('<your class name>', function () use ($abstract) {
    	return $abstract;
    });
  3. singleton The singleton method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:

    $abstract = new \Your\Class\Name;
    
    container()->singleton('<your class name>', function () use ($abstract) {
    	return $abstract;
    });
  4. scoped

    $abstract = new \Your\Class\Name;
    
    container()->scoped('<your class name>', function () use ($abstract) {
    	return $abstract;
    });
  5. get

    container()->get('<your class name>');
  6. make

    container('<your class name>');
    
    // or
    
    container()->make('<your class name>');
  7. call

    container()->call(['<your class name>', '<your class method name>'], ['<your class method vars name>' => '<your class method vars value>']);
    
    // or
    
    container()->call([new '<your class name>', '<your class method name>'], ['<your class method vars name>' => '<your class method vars value>']);
    
    // or
    
    container()->call(function (\Your\Class\Name $abstract) {
    	// do something
    });
  8. forgetScopedInstances Clear all of the scoped instances from the container.

  9. forgetInstance

    container()->forgetInstance('<your class name>');
  10. forgetInstances Clear all of the instances from the container.

  11. flush Flush the container of all bindings and resolved instances.