Skip to content

Commit 670f662

Browse files
author
Wazabii
committed
Guide
1 parent 5eca7bc commit 670f662

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@ Container, Factories and dependency injectors will help to make your PHP code mo
77
## Container
88
Containers allowing you to easily create and retrieve objects that are needed throughout your application.
99
```php
10-
use MaplePHP\Container\tests\TestClasses\TestClass;
11-
12-
$container->set("test1", TestClass::class); // Will load TestClass
13-
$container->set("test2", TestClass::class, ["Test"]); // Will load TestClass and set argumnet to constructor
14-
$container->set("test3", TestClass::class."::_get", ["Test 2"]); // Will load TestClass and static method named "_get" and set argumnet to that method
15-
16-
echo $container->get("test1")->get(); // Will echo @TestClass->get()
17-
echo $container->get("test2", ["Test 2 (overwritten)"])->get(); // Will echo @TestClass->get("Test 2 (overwritten)")
18-
echo $container->get("test3"); // Will echo @TestClass::_get()
10+
$container->set("YourClass", \YourNamespace\To\YourClass::class); // Bind "YourClass" to container and dependency injector
11+
$yourClass = $container->get("YourClass")->get(); // Will return "YourClass"
12+
//$yourClass->yourClassMehthod();
1913
```
14+
If the constructor of "YourClass" contains unresolved class arguments, the dependency injector will attempt to automatically locate them for you. Read more under the headline **dependency injector**.
15+
2016
## Factory
2117
Factories can be used to create new instances of objects, rather than instantiating them directly in your code.
2218
```php
@@ -35,24 +31,24 @@ You can use the **Dependency injector** just like create any other container, as
3531
Take a look at this example
3632

3733
```php
38-
use MaplePHP\Container\tests\TestClasses\TestClass;
39-
$container->set("uniqueKey", TestClass:class);
40-
// $container->set("uniqueKey", '\MaplePHP\Container\tests\Controllers\TestController'); // Same as above
41-
$testController = $container->get("uniqueKey");
42-
echo $testController->start();
34+
35+
$container->set("YourClass", \YourNamespace\To\YourClass::class);
36+
$testService = $container->get("YourClass");
37+
echo $testService->start();
4338

4439
```
45-
The above code will load **TestController** and auto initialize the class **Test**.
40+
The above code will load **YourClass** and auto initialize the class **Test**.
4641

4742
```php
48-
namespace MaplePHP\Container\tests\Controllers;
43+
namespace YourNamespace\To;
4944

50-
use MaplePHP\Container\tests\TestClasses\Test;
45+
use YourNamespace\ToTestClasses\Test;
5146

52-
class TestController {
47+
class YourClass {
5348

5449
private $test;
5550

51+
// Dependency injector will auto load "Test" class and the "Test" classes and so on.
5652
function __construct(Test $test) {
5753
$this->test = $test;
5854
}

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "maplephp/container",
3+
"type": "library",
4+
"description": "Container, Factories and dependency injectors with full PSR-11 support.",
5+
"keywords": ["container", "psr11", "factories", "dependency injectors", "injectors"],
6+
"homepage": "https://wazabii.se",
7+
"license": "Apache-2.0",
8+
"authors": [
9+
{
10+
"name": "Daniel Ronkainen",
11+
"email": "[email protected]"
12+
},
13+
{
14+
"name": "MaplePHP",
15+
"homepage": "https://wazabii.se"
16+
}
17+
],
18+
"require": {
19+
"php": ">=8.0",
20+
"maplephp/dto": "^1.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"MaplePHP\\Container\\": ""
25+
}
26+
},
27+
"minimum-stability": "dev"
28+
}

0 commit comments

Comments
 (0)