Reputation: 3731
I am trying to register my own class as a services with help of symfony dependency injection component, but i have problems with class loading.
I have file structure as this:
My Generator class is simple
<?php
namespace Localhost\Service\String;
class Generator {
private $iStringLength;
public function __construct($iNewStringLength = 5) {
$this->iStringLength = $iNewStringLength;
}
public function getRandomString() {
$sChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$sRandChar = substr(str_shuffle(str_repeat($sChars,5)),0, $this->iStringLength);
return $sRandChar;
}
}
And Index is
<?php
require_once 'vendor/autoload.php';
/*
spl_autoload_register(function ($sClass) {
echo $sClass;
require_once str_replace('\\', '/', $sClass) . '.php';
});
*/
use Localhost\Service\String\Generator;
/*
$oStringGenerator = new Generator(55);
echo $oStringGenerator->getRandomString();
*/
use Symfony\Component\DependencyInjection\ContainerBuilder;
$oContainer = new ContainerBuilder();
$oContainer
->register('generator', 'Generator')
->addArgument('15');
$oGeneratorService = $oContainer->get('generator');
echo $oGeneratorService->getRandomString();
What i am getting is an error
Fatal error: Uncaught exception 'ReflectionException' with message 'Class Generator does not exist' in D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php:959 Stack trace: #0 D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php(959): ReflectionClass->__construct('Generator') #1 D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php(493): Symfony\Component\DependencyInjection\ContainerBuilder->createService(Object(Symfony\Component\DependencyInjection\Definition), 'generator') #2 D:\Localhost\Apache\htdocs\Test\index.php(26): Symfony\Component\DependencyInjection\ContainerBuilder->get('generator') #3 {main} thrown in D:\Localhost\Apache\htdocs\Test\vendor\symfony\dependency-injection\Symfony\Component\DependencyInjection\ContainerBuilder.php on line 959
Or as a picture
Upvotes: 1
Views: 1374
Reputation: 24280
Edit: Since Symfony 3.3+ (May 2017) you can use register()
class name service shortcut:
$containerBuilder = new ContainerBuilder();
$containerBuilder->register(Localhost\Service\String\Generator::class)
->addArgument('15');
Since PHP 5.5+ you can use more fail-proof ::class
notation:
$containerBuilder = new ContainerBuilder();
$containerBuilder->register('generator', Localhost\Service\String\Generator::class)
->addArgument('15');
Now, when class name will be miss-typed, your IDE will highlight it.
Upvotes: 1
Reputation: 52473
addition:
You should propbably compile the container for performance reasons.
$container = new ContainerBuilder();
$container
->register('generator', 'Localhost\Service\String\Generator')
->addArgument('15')
;
$container->compile();
Upvotes: 0
Reputation: 49533
$oContainer = new ContainerBuilder();
$oContainer
->register('generator', 'Localhost\Service\String\Generator')
->addArgument('15');
Upvotes: 2
Reputation: 3731
Solution is simple, i forgot to modify composer config to load my services
"autoload": {
"psr-0": {"Localhost": "src/"}
},
Upvotes: 2