https://github.com/sciactive/requirephp
An implementation of dependency injection/service location (like RequireJS) in PHP.
https://github.com/sciactive/requirephp
dependency-injection php
Last synced: 15 days ago
JSON representation
An implementation of dependency injection/service location (like RequireJS) in PHP.
- Host: GitHub
- URL: https://github.com/sciactive/requirephp
- Owner: sciactive
- License: lgpl-3.0
- Created: 2014-05-03T09:01:53.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2016-07-02T22:45:21.000Z (over 9 years ago)
- Last Synced: 2026-03-01T07:33:41.240Z (22 days ago)
- Topics: dependency-injection, php
- Language: PHP
- Homepage: http://requirephp.org
- Size: 838 KB
- Stars: 24
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#
RequirePHP
[](https://packagist.org/packages/sciactive/requirephp) [](https://packagist.org/packages/sciactive/requirephp) [](https://packagist.org/packages/sciactive/requirephp) [](https://github.com/sciactive/requirephp/issues)
An implementation of dependency injection and service locator (like RequireJS) in PHP.
## Installation
You can install RequirePHP with Composer or Bower.
```sh
composer require sciactive/requirephp
bower install https://github.com/sciactive/requirephp.git
```
## Getting Started
If you don't use an autoloader, all you need to do is include the RequirePHP.php file.
```php
require("RequirePHP.php");
```
Now you can start giving code that requires a module, or modules, to run. This code will not run until all the required modules (in this case, only 'test') are available.
```php
\SciActive\RequirePHP::_(array('test'), function($test){
$test->value = '
Hello, world.
';
});
```
You can define modules. This module has no dependencies, hence the empty array.
```php
\SciActive\RequirePHP::_('test', array(), function(){
class test {
public $value;
public function talk() {
echo $this->value;
}
}
// Returning a new instantiation is important if you are
// providing a service.
return new test();
});
```
You can create aliases to modules (and other aliases).
```php
\SciActive\RequirePHP::alias('testing', 'test');
```
You can keep using the same instance in other code, using RequirePHP as a service locator. This function uses the alias from above.
```php
\SciActive\RequirePHP::_(array('testing'), function($test){
$test->talk(); // Prints '
Hello, world.
'.
});
```
You can also retrieve modules outside of a closure. However, if this module is not available at the time you request it, RequirePHP will throw a RequireModuleFailedException. Such is the price of not using a closure.
```php
$test = \SciActive\RequirePHP::_('test');
$test->talk(); // Prints '
Hello, world.
'.
```
## Service Location
The repository contains [an example](https://github.com/sciactive/requirephp/blob/master/test_service_locator.php) of using RequirePHP as a service locator.
## Dependency Injection
The repository contains [an example](https://github.com/sciactive/requirephp/blob/master/test_dependency_injector.php) of using RequirePHP as a dependency injector.
## Contacting the Developer
There are several ways to contact RequirePHP's developer with your questions, concerns, comments, bug reports, or feature requests.
- RequirePHP is part of [SciActive on Twitter](http://twitter.com/SciActive).
- Bug reports, questions, and feature requests can be filed at the [issues page](https://github.com/sciactive/requirephp/issues).
- You can directly [email Hunter Perrin](mailto:hunter@sciactive.com), the creator of RequirePHP.