PHPOffice/PHPWord · error · PhpOffice\PhpWord\Exception\Exception

Invalid part type

Error message

Invalid part type: {$type}

What it means

EPub3 writer parts are resolved dynamically: getPartClass() maps a part type string to a class under PhpOffice\PhpWord\Writer\EPub3\Part\. If the class does not exist the type string is not a supported part and the method throws. This guards against typos and unknown part names.

Solutions

  1. Use only part types whose classes exist under src/PhpWord/Writer/EPub3/Part/.
  2. Fix capitalization — class names are case-sensitive ('Content', not 'content').
  3. If adding a custom part, create the class in the Part namespace before resolving it.
  4. Validate the type with class_exists before calling getPartClass.

Example fix

// before
$class = Part::getPartClass('content'); // class PhpOffice\...\Part\content does not exist -> throws
// after
$class = Part::getPartClass('Content'); // matches PhpOffice\PhpWord\Writer\EPub3\Part\Content
Defensive patterns

Strategy: validation

Validate before calling

$validTypes = array_map(
    fn($f) => pathinfo($f, PATHINFO_FILENAME),
    glob(__DIR__ . '/vendor/phpoffice/phpword/src/PhpWord/Writer/EPub3/Part/*.php')
);
$valid = in_array($type, $validTypes, true);

Type guard

function isValidEpub3PartType(string $type): bool {
    return class_exists('PhpOffice\\PhpWord\\Writer\\EPub3\\Part\\' . $type);
}

Try / catch

try {
    $class = Part::getPartClass($type);
} catch (\PhpOffice\PhpWord\Exception\Exception $e) {
    if (str_starts_with($e->getMessage(), 'Invalid part type')) {
        $class = Part::getPartClass('Content'); // safe default
    } else { throw $e; }
}

Prevention

When it happens

Trigger: Calling Part::getPartClass() with a type that has no matching class in the EPub3 Part namespace — misspelled names, wrong capitalization ('content' vs 'Content'), or a custom part whose class was never created.

Common situations: Custom extension code adding new EPub3 parts before creating the class; typos in the type argument; case-sensitivity mistakes; upgrading PhpWord where part classes were renamed.

Related errors


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

Appendix: source

Thrown at src/PhpWord/Writer/EPub3/Part.php:40

/**
 * Factory class for EPub3 parts.
 */
class Part
{
    /**
     * Get the fully qualified class name for a specific part type.
     *
     * @param string $type The type of part (Content, Manifest, Meta, Mimetype)
     *
     * @return string The fully qualified class name
     */
    public static function getPartClass(string $type): string
    {
        $class = 'PhpOffice\\PhpWord\\Writer\\EPub3\\Part\\' . $type;

        if (!class_exists($class)) {
            throw new Exception("Invalid part type: {$type}");
        }

        return $class;
    }
}

View on GitHub (pinned to aef95c0415)