PHPOffice/PHPWord · error · BadMethodCallException

Method $function is not defined.

Error message

Method $function is not defined.

What it means

PhpWord's magic __call forwards unknown method names to static Style methods, but only for whitelisted add*Style names. Any other undefined method name hits the BadMethodCallException, meaning the method does not exist on PhpWord or the Style facade.

Solutions

  1. Check the method name against PhpWord's supported add*Style methods (addFontStyle, addParagraphStyle, addTitleStyle, addTableStyle, addNumberingStyle, addHeadingStyle, addCellStyle, addLinkStyle)
  2. Use getStyle()/styles API directly (Style::setFont, etc.) if the shortcut does not exist
  3. Check the PhpWord version documentation for renamed methods

Example fix

// before
$phpWord->addParagraphStile('myStyle', ['spacing' => 100]); // typo
// after
$phpWord->addParagraphStyle('myStyle', ['spacing' => 100]);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!method_exists($phpWord, $methodName) && !in_array($methodName, ['addFontStyle','addParagraphStyle','addTitleStyle','addTableStyle','addNumberingStyle','addHeadingStyle','addCellStyle','addLinkStyle'], true)) { throw new \BadMethodCallException($methodName); }

Type guard

function isPhpWordStyleMethod(string $name): bool { return (bool) preg_match('/^(add|set)[A-Z]/', $name) && method_exists(\PhpOffice\PhpWord\PhpWord::class, $name); }

Try / catch

try { $phpWord->$method(...$args); } catch (\BadMethodCallException $e) { // fall back to documented style API }

Prevention

When it happens

Trigger: Calling an undefined method on the PhpWord instance, e.g. $phpWord->addWrongStyle() or a typo like $phpWord->addSectionStyle() instead of the supported add*Style methods (addFontStyle, addParagraphStyle, etc.).

Common situations: Typo in a style method name; copying code from an older/newer PhpWord version where the method was renamed or removed; expecting instance style methods that were never part of the API.

Related errors


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/c24da760146d89b6. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/PhpWord.php:149

            return $this->collections[$key];
        }

        // Run add collection item method
        if (in_array($function, $addCollection)) {
            $key = ucfirst(str_replace('add', '', $function) . 's');

            $collectionObject = $this->collections[$key];

            return $collectionObject->addItem($args[0] ?? null);
        }

        // Run add style method
        if (in_array($function, $addStyle)) {
            return forward_static_call_array(['PhpOffice\\PhpWord\\Style', $function], $args);
        }

        // Exception
        throw new BadMethodCallException("Method $function is not defined.");
    }

    /**
     * Get document properties object.
     *
     * @return Metadata\DocInfo
     */
    public function getDocInfo()
    {
        return $this->metadata['DocInfo'];
    }

    /**
     * Get compatibility.
     *
     * @return Metadata\Compatibility
     *
     * @since 0.12.0

View on GitHub (pinned to aef95c0415)