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

No PhpWord assigned.

Error message

No PhpWord assigned.

What it means

A state guard in ContentXhtml::write(): the EPub3 content part's $phpWord property was never set, so there is no document to serialize into XHTML. This fires when write() is called on the part without first assigning a PhpWord instance (typically via the parent's setPhpWord()), e.g. when a part is written out of order or constructed standalone instead of through the normal EPub3 writer pipeline.

Solutions

  1. Always obtain the part via the EPub3 writer (getWriterPart) so setPhpWord() is called for you.
  2. Call setPhpWord($phpWord) on the part before invoking write().
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at src/PhpWord/Writer/EPub3/Part/ContentXhtml.php:50 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at src/PhpWord/Writer/EPub3/Part/ContentXhtml.php:50

     * Get XML Writer.
     *
     * @return XMLWriter
     */
    protected function getXmlWriter()
    {
        $xmlWriter = new XMLWriter();
        $xmlWriter->openMemory();

        return $xmlWriter;
    }

    /**
     * Write part content.
     */
    public function write(): string
    {
        if ($this->phpWord === null) {
            throw new \PhpOffice\PhpWord\Exception\Exception('No PhpWord assigned.');
        }

        $xmlWriter = $this->getXmlWriter();

        $xmlWriter->startDocument('1.0', 'UTF-8');
        $xmlWriter->startElement('html');
        $xmlWriter->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
        $xmlWriter->writeAttribute('xmlns:epub', 'http://www.idpf.org/2007/ops');
        $xmlWriter->startElement('head');
        $xmlWriter->writeElement('title', $this->phpWord->getDocInfo()->getTitle() ?: 'Untitled');
        $xmlWriter->endElement(); // head
        $xmlWriter->startElement('body');

        // Write sections content
        foreach ($this->phpWord->getSections() as $section) {
            $xmlWriter->startElement('div');
            $xmlWriter->writeAttribute('class', 'section');

View on GitHub (pinned to aef95c0415)