I just recently got an error, having to do with deprecation, by using the type of calls in the other example listed here. What I had to do instead looks like this...
$htmldoc = (new DOMImplementation)->createDocument(null, 'html', (new DOMImplementation)->createDocumentType("html"));
This creates a document with <!DOCTYPE html> at the top of it.