{"record":{"id":"f21215df4978a8f3","repo":"phalcon/cphalcon","slug":"no-factory-set-for-service-name","errorCode":null,"errorMessage":"No factory set for service '{name}'","messagePattern":"No factory set for service '(.+?)'","errorType":"exception","errorClass":"Phalcon\\Container\\Exceptions\\NoFactorySet","httpStatus":null,"severity":"error","filePath":"phalcon/Container/Definition/ServiceDefinition.zep","lineNumber":275,"sourceCode":"     * Returns the extenders\n     *\n     * @return array<array-key, callable>\n     */\n    public function getExtenders() -> array\n    {\n        return this->extenders;\n    }\n\n    /**\n     * Returns the factory\n     *\n     * @return callable\n     * @throws NoFactorySet\n     */\n    public function getFactory() -> callable\n    {\n        if (this->factory === null) {\n            throw new NoFactorySet(this->serviceName);\n        }\n\n        return this->factory;\n    }\n\n    /**\n     * Returns the lifetime\n     *\n     * @return string\n     */\n    public function getLifetime() -> string\n    {\n        return this->lifetime;\n    }\n\n    /**\n     * Returns the name of the service\n     *","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Container/Definition/ServiceDefinition.zep#L257-L293","documentation":"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.","triggerScenarios":"getFactory() on a definition registered via set('logger', Logger::class); calling getFactory() on a fresh newDefinition() before setFactory(); definitions configured with setClass()/setConstructorArgs() only.","commonSituations":"Generic code assuming all definitions are closure-based; refactoring from factories to class-based definitions; inspection tooling reading factories of every service.","solutions":["Guard with $definition->hasFactory() before getFactory()","If a factory is required, set one: $def->setFactory(fn ($container) => new Logger($container->get('log')))","For class definitions use getClass()/buildService() semantics instead of getFactory()","Remember the ObjectProcessor already sets a factory returning the stored object, so only class definitions lack one"],"exampleFix":"// before\n$factory = $container->getDefinition('logger')->getFactory(); // NoFactorySet\n\n// after\n$def = $container->getDefinition('logger');\n$factory = $def->hasFactory()\n    ? $def->getFactory()\n    : fn ($c) => new $def->getClass()(...$def->getConstructorArgs());","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function definitionFactoryOrNull(\\Phalcon\\Container\\Definition\\ServiceDefinition $def): ?callable\n{\n    return $def->hasFactory() ? $def->getFactory() : null;\n}","tryCatchPattern":"use Phalcon\\Container\\Exceptions\\NoFactorySet;\n\ntry {\n    $factory = $def->getFactory();\n} catch (NoFactorySet $e) {\n    // class-based definition: build via buildService()/container->get() instead\n}","preventionTips":["Always pair getFactory() with hasFactory()","Class-based (string) definitions intentionally have no factory — reflect and build instead","If generic code needs a factory for every definition, synthesize one from getClass()"],"tags":["di-container","definition","factory","phalcon"],"backgroundTag":"invalid-container-definition","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}