{"record":{"id":"4232b0873597359c","repo":"yiisoft/yii2","slug":"calling-unknown-method-class-name-4232b0","errorCode":null,"errorMessage":"Calling unknown method: {class}::{name}()","messagePattern":"Calling unknown method: (.+?)::(.+?)\\(\\)","errorType":"exception","errorClass":"UnknownMethodException","httpStatus":null,"severity":"error","filePath":"framework/base/Component.php","lineNumber":316,"sourceCode":"     * This method will check if any attached behavior has\n     * the named method and will execute it if available.\n     *\n     * Do not call this method directly as it is a PHP magic method that\n     * will be implicitly called when an unknown method is being invoked.\n     * @param string $name the method name\n     * @param array $params method parameters\n     * @return mixed the method return value\n     * @throws UnknownMethodException when calling unknown method\n     */\n    public function __call($name, $params)\n    {\n        $this->ensureBehaviors();\n        foreach ($this->_behaviors as $object) {\n            if ($object->hasMethod($name)) {\n                return call_user_func_array([$object, $name], $params);\n            }\n        }\n        throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . \"::$name()\");\n    }\n\n    /**\n     * This method is called after the object is created by cloning an existing one.\n     * It removes all behaviors because they are attached to the old object.\n     */\n    public function __clone()\n    {\n        $this->_events = [];\n        $this->_eventWildcards = [];\n        $this->_behaviors = null;\n    }\n\n    /**\n     * Returns a value indicating whether a property is defined for this component.\n     *\n     * A property is defined if:\n     *","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/base/Component.php#L298-L334","documentation":"For Components, __call() first scans attached behaviors and executes the first one that has the method; only when no behavior provides it does it throw UnknownMethodException('Calling unknown method: C::name()'). On a Component this error therefore means neither the class itself nor any attached behavior implements the method - and the most common real cause is an expected behavior that was never attached.","triggerScenarios":"Calling $model->upload() (provided by an UploadBehavior) when behaviors() contains a typo'd class name, a wrong config shape, or the behavior attaches conditionally (a when() callback returning false); calling behavior methods on a clone (behaviors were cleared); behavior attach delayed behind an event that has not fired yet.","commonSituations":"Slug/Timestamp/Upload behaviors configured per model; behavior config supplied by a module that differs between tests and production; refactors that moved behaviors() out of the model class.","solutions":["Dump $model->getBehaviors() at runtime and compare with what you expect - an empty list or a missing entry is the usual answer","Fix the behaviors() declaration, e.g. return ['slug' => ['class' => SlugBehavior::class]]","If the behavior is attached elsewhere (module or event), ensure that code runs before the call","Otherwise call the behavior instance directly or move the method onto the class"],"exampleFix":"// before\nclass Post extends \\yii\\db\\ActiveRecord\n{\n    // behaviors() forgotten\n}\n$post->makeSlug(); // UnknownMethodException\n\n// after\nclass Post extends \\yii\\db\\ActiveRecord\n{\n    public function behaviors()\n    {\n        return ['slug' => ['class' => \\app\\behaviors\\SlugBehavior::class]];\n    }\n}","handlingStrategy":"type-guard","validationCode":"if ($component->hasMethod('upload')) { // Component::hasMethod checks attached behaviors\n    $component->upload();\n} else {\n    throw new \\BadMethodCallException('UploadBehavior is not attached');\n}","typeGuard":"function behaviorProvides(\\yii\\base\\Component $component, string $method): bool\n{\n    $component->ensureBehaviors();\n    return $component->hasMethod($method);\n}","tryCatchPattern":"try {\n    $model->makeSlug();\n} catch (\\yii\\base\\UnknownMethodException $e) {\n    // behavior not attached - degrade gracefully or attach and retry\n    $model->attachBehavior('slug', \\app\\behaviors\\SlugBehavior::class);\n    $model->makeSlug();\n}","preventionTips":["Keep behaviors() declarations declarative and typo-checked (use ::class constants)","Test each behavior-provided method in the model's test suite","Remember clone detaches behaviors; reattach or use a factory instead of clone"],"tags":["oop","behaviors","magic-methods","method-call"],"backgroundTag":"undefined-method-call","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}