phalcon/cphalcon · error · Phalcon\Container\Exceptions\NoFactorySet

No factory set for service '{name}'

Error message

No factory set for service '{name}'

What it means

ServiceDefinition::getFactory() throws NoFactorySet when no callable factory was stored. Class-based (string) definitions normally have no factory — buildService() reflects and instantiates the class instead — so calling getFactory() on them is the typical trigger. hasFactory() is the safe pre-check.

Source

Thrown at phalcon/Container/Definition/ServiceDefinition.zep:275

     * Returns the extenders
     *
     * @return array<array-key, callable>
     */
    public function getExtenders() -> array
    {
        return this->extenders;
    }

    /**
     * Returns the factory
     *
     * @return callable
     * @throws NoFactorySet
     */
    public function getFactory() -> callable
    {
        if (this->factory === null) {
            throw new NoFactorySet(this->serviceName);
        }

        return this->factory;
    }

    /**
     * Returns the lifetime
     *
     * @return string
     */
    public function getLifetime() -> string
    {
        return this->lifetime;
    }

    /**
     * Returns the name of the service
     *

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Guard with $definition->hasFactory() before getFactory()
  2. If a factory is required, set one: $def->setFactory(fn ($container) => new Logger($container->get('log')))
  3. For class definitions use getClass()/buildService() semantics instead of getFactory()
  4. Remember the ObjectProcessor already sets a factory returning the stored object, so only class definitions lack one

Example fix

// before
$factory = $container->getDefinition('logger')->getFactory(); // NoFactorySet

// after
$def = $container->getDefinition('logger');
$factory = $def->hasFactory()
    ? $def->getFactory()
    : fn ($c) => new $def->getClass()(...$def->getConstructorArgs());
Defensive patterns

Strategy: type-guard

Type guard

function definitionFactoryOrNull(\Phalcon\Container\Definition\ServiceDefinition $def): ?callable
{
    return $def->hasFactory() ? $def->getFactory() : null;
}

Try / catch

use Phalcon\Container\Exceptions\NoFactorySet;

try {
    $factory = $def->getFactory();
} catch (NoFactorySet $e) {
    // class-based definition: build via buildService()/container->get() instead
}

Prevention

When it happens

Trigger: getFactory() on a definition registered via set('logger', Logger::class); calling getFactory() on a fresh newDefinition() before setFactory(); definitions configured with setClass()/setConstructorArgs() only.

Common situations: Generic code assuming all definitions are closure-based; refactoring from factories to class-based definitions; inspection tooling reading factories of every service.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/f21215df4978a8f3. Report an issue: GitHub.