1

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:

enter image description here

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

enter image description here

2
  • 1
    This is symfony2 project? Or you wanna use only DependencyInjection Component? Commented Feb 25, 2014 at 10:08
  • Only DependencyInjection component Commented Feb 25, 2014 at 10:17

4 Answers 4

2
$oContainer = new ContainerBuilder();
$oContainer
    ->register('generator', 'Localhost\Service\String\Generator')
    ->addArgument('15');
Sign up to request clarification or add additional context in comments.

Comments

2

Solution is simple, i forgot to modify composer config to load my services

"autoload": {
    "psr-0": {"Localhost": "src/"}
},

Comments

1

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.

Comments

0

addition:

You should propbably compile the container for performance reasons.

$container = new ContainerBuilder();
$container
    ->register('generator', 'Localhost\Service\String\Generator')
    ->addArgument('15')
;
$container->compile();

2 Comments

Now it generates fatal, "Fatal error: Class 'Symfony\Component\Config\Resource\FileResource' not found" :) But its understandable, i have no config component included.
Oh that's odd. I think it should probably throw a more descriptive exception in case symfony/config isn't installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.