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

No class set for service '{name}'

Error message

No class set for service '{name}'

What it means

ServiceDefinition::getClass() returns the stored class name and throws NoClassSet when the definition has none. Definitions created from closures or pre-built objects (ClosureProcessor/ObjectProcessor) only carry a factory, never a class name, so getClass() is invalid for them. The companion hasClass() exists exactly for this check.

Source

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

     * Returns the arguments
     *
     * @return array
     */
    public function getArguments() -> array
    {
        return this->arguments;
    }

    /**
     * Returns the class
     *
     * @return string
     * @throws NoClassSet
     */
    public function getClass() -> string
    {
        if (this->className === null) {
            throw new NoClassSet(this->serviceName);
        }

        return this->className;
    }

    /**
     * Returns the constructor arguments
     *
     * @return array
     */
    public function getConstructorArgs() -> array
    {
        return this->constructorArgs;
    }

    /**
     * Returns the extenders
     *

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Guard with $definition->hasClass() before calling getClass()
  2. If the definition should instantiate a class, call setClass(ClassName::class)
  3. If you need the instance rather than metadata, use the container's get()/buildService() path instead
  4. Treat hasClass() false + hasFactory() true as a normal factory-style definition

Example fix

// before
$class = $container->getDefinition('helper')->getClass(); // NoClassSet

// after
$def = $container->getDefinition('helper');
$class = $def->hasClass() ? $def->getClass() : null;
Defensive patterns

Strategy: type-guard

Type guard

function definitionClassOrNull(\Phalcon\Container\Definition\ServiceDefinition $def): ?string
{
    return $def->hasClass() ? $def->getClass() : null;
}

Try / catch

use Phalcon\Container\Exceptions\NoClassSet;

try {
    $class = $def->getClass();
} catch (NoClassSet $e) {
    $class = null; // factory/object definition: use hasFactory()/getFactory() instead
}

Prevention

When it happens

Trigger: Calling getClass() on a definition returned by set('x', fn () => new Thing()); calling getClass() on a definition built with newDefinition() before setClass(); generic code that iterates all definitions assuming each has a class.

Common situations: Debug panels, compilers, or tooling that reflect over every definition; refactoring a service from class-based to factory-based without updating the code that reads getClass().

Related errors


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