{"record":{"id":"19c3c453a2fc9328","repo":"phalcon/cphalcon","slug":"service-name-not-registered","errorCode":null,"errorMessage":"Service '{name}' not registered","messagePattern":"Service '(.+?)' not registered","errorType":"exception","errorClass":"Phalcon\\Container\\Exceptions\\ServiceNotRegistered","httpStatus":null,"severity":"error","filePath":"phalcon/Container/Container.zep","lineNumber":318,"sourceCode":"    }\n\n    /**\n     * Resolve an return a service\n     *\n     * @param string $serviceName\n     *\n     * @return object\n     * @throws ServiceNotFound\n     * @throws ServiceNotRegistered\n     */\n    public function getService(string serviceName) -> object\n    {\n        var result;\n\n        let result = this->get(serviceName);\n\n        if (!is_object(result)) {\n            throw new ServiceNotRegistered(serviceName);\n        }\n\n        return result;\n    }\n\n    /**\n     * Returns the names of every registered service definition. Names that\n     * only exist as an alias, a pre-set instance or a parameter are not\n     * included.\n     *\n     * @return array<int, string>\n     */\n    public function getServiceNames() -> array\n    {\n        return array_keys(this->services);\n    }\n\n    /**","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Container/Container.zep#L300-L336","documentation":"getService() calls get() and then enforces that the resolved value is a PHP object; ServiceNotRegistered is thrown when the name resolves fine but to a scalar, array, or null. With this container's processors, the usual source is a closure definition whose factory returns a non-object.","triggerScenarios":"$container->set('settings', fn () => ['debug' => true]); then $container->getService('settings') — the closure returns an array; likewise a closure returning a string, int, or null.","commonSituations":"Storing configuration arrays or primitives as container entries (set() allows any closure return) and later handing them to code that requires a service object; generic code paths that assume every container entry is an object.","solutions":["Store non-object values as parameters: setParameter('settings', [...]) and read with getParameter()","Make the factory return an object (e.g. a Config/DTO object) if consumers genuinely need a service","Use get() instead of getService() when a non-object result is legitimate for that name","Guard manually: $result = $container->get('settings'); if (!is_object($result)) { /* parameter path */ }"],"exampleFix":"// before\n$container->set('settings', fn () => ['debug' => true]);\n$settings = $container->getService('settings'); // ServiceNotRegistered\n\n// after\n$container->setParameter('settings', ['debug' => true]);\n$settings = $container->getParameter('settings');","handlingStrategy":"type-guard","validationCode":"$result = $container->get('settings');\nif (!is_object($result)) {\n    // non-object entry: treat as value/parameter, do not call getService()\n    throw new LogicException(\"'settings' does not resolve to an object.\");\n}","typeGuard":"function resolvesToObject(\\Phalcon\\Container\\Container $container, string $name): bool\n{\n    return is_object($container->get($name));\n}","tryCatchPattern":"use Phalcon\\Container\\Exceptions\\ServiceNotRegistered;\n\ntry {\n    $svc = $container->getService('settings');\n} catch (ServiceNotRegistered $e) {\n    // entry is a value, not a service object; fall back to get()/getParameter()\n}","preventionTips":["Keep a convention: services resolve to objects, values go into parameters","Do not hand container entries to object-typed APIs without checking the resolution result","Prefer get() + is_object() checks in generic code instead of getService()"],"tags":["di-container","type-mismatch","service-not-registered","phalcon"],"backgroundTag":"service-not-registered","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}