phpshots / bind-it
A binding component for PHP applications, enabling efficient registration and resolution of abstract types.
Requires
- php: ^7.4 || ^8.0
- phpshots/common-type-alias: ^0.1.1
- psr/container: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.5 || ^10.0
This package is auto-updated.
Last update: 2025-02-19 17:06:37 UTC
README

PHPShots/bint-it [Dependecies Injection Manager]
A common library for managing Dependecies Injection in PHP.
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
PHPShots Bind-It
phpshots/bind-it
is a powerful and lightweight PHP container library that simplifies dependency injection and contextual bindings. It provides intuitive methods to manage singleton instances, contextual bindings, and dependency resolution in your PHP applications.
Why Use Bind-It?
-
Flexibility and Control: Define how your application resolves dependencies with complete control over bindings, contextual mappings, and singleton instances.
-
Simplicity: Clean and intuitive API for managing dependencies, allowing developers to focus on application logic.
-
Contextual Binding: Bind different implementations of a dependency based on the requesting class or context.
-
Scalability: Suitable for both small applications and large-scale systems, adapting seamlessly to growth.
-
Integration-Friendly: Lightweight and easy to integrate into any PHP project or framework.
-
Testing Made Easy: Improves testability by decoupling dependencies, making integration with PHPUnit or other testing frameworks seamless.
Features
- Bind and Resolve: Easily bind classes or interfaces to implementations and resolve them when needed.
- Singletons: Manage single instances across the application lifecycle.
- Contextual Binding: Bind different implementations of a dependency based on the requesting class or context.
- Automatic Resolution: Automatically resolve class dependencies using reflection.
- Flexible Configuration: Adapt bindings for varying application needs.
Getting Started
Follow these steps to set up and start using the Bind-it library in your PHP project.
Prerequisites
Before you begin, ensure you have the following installed:
- PHP (version 8.2 or higher)
apt install php
- Composer (for dependency management)
apt install composer
Installation
-
Clone the Repository: Start by cloning the repository to your local machine:
git clone https://github.com/hakrichTech/bindIt.git
-
Navigate to the Project Directory
cd bindIt
-
Install Dependencies: Use Composer to install the required dependencies:
composer install
-
Or install the package using composer:
composer require phpshots/bind-it
Basic Setup
-
Include the Library: In your PHP script, include the Composer autoload file to access the Bind-it library:
require 'vendor/autoload.php'; // Adjust the path as necessary
-
Binding and Resolving Services: Bind a class or interface to a concrete implementation:
use PHPShots\Common\Container; $container = new Container(); $container->bind('ConditionalService', function ($container) { return new ConditionalService(); }); $service = $container->make('ConditionalService');
-
Singleton Binding: Ensure only one instance of a class is shared across the application:
$container->singleton('GreetingService', GreetingService::class); $instance1 = $container->make('GreetingService'); $instance2 = $container->make('GreetingService'); if($instance1 === $instance2):; // Both instances are the same endif;
-
Contextual Binding: Provide different implementations of a dependency based on the requesting class:
use PHPShots\Common\ContextualBindingBuilder; $contextualBinding = new ContextualBindingBuilder($container, ConcreteClass::class); $contextualBinding->needs(AbstractClass::class)->give(ConcreteImplementation::class); $instance = $container->make(ConcreteClass::class); if($instance->getDependency() instanceof ConcreteImplementation): { echo true; }
Or use shorthand:
$container->addContextualBinding(ConcreteClass::class, AbstractClass::class, ConcreteImplementation::class);
-
Resolving with Dependencies: Automatically resolve dependencies for complex class hierarchies:
$container->bind(AbstractClass::class, ConcreteImplementation::class); $container->bind(ConcreteClass::class, ClassWithDependency::class); $resolved = $container->make(ConcreteClass::class); if($resolved instanceof ClassWithDependency){ echo true; } if($resolved->getDependency() instanceof ConcreteImplementation){ echo true; }
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt
for more information.
Contact
Shamavu Rasheed - @hakeem-shamavu - shamavurasheed@gmail.com
Project Link: https://github.com/hakrichTech/bindIt
Acknowledgments
Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!