PHPOffice/PHPWord · error · Exception
" " is not a valid .
Error message
"{$name}" is not a valid {$type}. What it means
IOFactory::createObject() maps "PhpOffice\PhpWord\{$type}\{$name}" to a class (used by createReader with type 'Reader'). If the class does not exist or is not a concrete instantiable class, a PhpWord Exception is thrown. Effectively a whitelist+autoloader check for reader names like 'Word2007'.
Solutions
- Use an exact supported reader name: 'Word2007', 'ODText', 'RTF', 'HTML' (check src/PhpWord/Reader/ for available classes).
- Ensure composer dump-autoload has run and the PhpWord package is installed correctly.
- Verify the requested reader class exists and is concrete (createObject checks isConcreteClass).
- Catch PhpWord Exception around createReader for dynamic format names.
Example fix
// before
$reader = IOFactory::createReader('docx');
// after
$reader = IOFactory::createReader('Word2007'); Defensive patterns
Strategy: validation
Validate before calling
$readers = ['Word2007', 'ODText', 'RTF', 'HTML']; // classes present in src/PhpWord/Reader
if (!in_array($name, $readers, true)) { throw new DomainException('Unsupported reader'); } Type guard
function readerClassExists(string $name): bool { return class_exists('PhpOffice\\PhpWord\\Reader\\' . $name) && (new \ReflectionClass('PhpOffice\\PhpWord\\Reader\\' . $name))->isInstantiable(); } Try / catch
try { $reader = IOFactory::createReader($name); } catch (\PhpOffice\PhpWord\Exception\Exception $e) { log('unsupported reader: ' . $e->getMessage()); } Prevention
- Use reader names matching existing Reader classes exactly
- Run composer dump-autoload after install/update
- Don't expect readers for formats the library only writes (e.g. PDF)
When it happens
Trigger: IOFactory::createReader('docx') (lowercase → class PhpWord\Reader\docx missing), createReader('ODText') where the Reader\ODText class doesn't exist, or any name whose corresponding Reader class fails to autoload or is abstract/interface.
Common situations: Using format names/aliases that don't match a Reader class, typos, expecting readers for formats the library can't read (e.g. PDF), or composer autoloading not generating classmaps.
Related errors
- " " is not a valid writer.
- Cannot read .
- Cannot read .
- Invalid part type
- Invalid value, on of ' . implode(', ', $position) . '…
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/8cf2a4e8c0cd52cc.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/IOFactory.php:76
}
/**
* Create new object.
*
* @param string $type
* @param string $name
* @param PhpWord $phpWord
*
* @return ReaderInterface|WriterInterface
*/
private static function createObject($type, $name, $phpWord = null)
{
$class = "PhpOffice\\PhpWord\\{$type}\\{$name}";
if (class_exists($class) && self::isConcreteClass($class)) {
return new $class($phpWord);
}
throw new Exception("\"{$name}\" is not a valid {$type}.");
}
/**
* Loads PhpWord from file.
*
* @param string $filename The name of the file
* @param string $readerName
*
* @return PhpWord $phpWord
*/
public static function load($filename, $readerName = 'Word2007')
{
/** @var ReaderInterface $reader */
$reader = self::createReader($readerName);
return $reader->load($filename);
}
View on GitHub (pinned to aef95c0415)