(PHP 5, PHP 7, PHP 8)

DOMImplementation::createDocument Creates a DOMDocument object of the specified type with its document element


public DOMImplementation::createDocument(?string $namespace = null, string $qualifiedName = "", ?DOMDocumentType $doctype = null): DOMDocument|false

Creates a DOMDocument object of the specified type with its document element.

Elenco dei parametri


The namespace URI of the document element to create.


The qualified name of the document element to create.


The type of document to create or null.

Valori restituiti

A new DOMDocument object or false on error. If namespace, qualifiedName, and doctype are null, the returned DOMDocument is empty with no document element



Raised if doctype has already been used with a different document or was created from a different implementation.


Raised if there is an error with the namespace, as determined by namespace and qualifiedName.

Questo metodo può essere chiamato in modo statico, ma genererà un errore E_STRICT.

Log delle modifiche

Versione Descrizione
8.0.3 namespace is now nullable.
8.0.0 doctype is now nullable.

Vedere anche:

add a note add a note

User Contributed Notes 3 notes

12 years ago
To add on to the other example, here's how to create an XHTML 1.0 transitional document with head, title, and body elements.


= DOMImplementation::createDocument(null, 'html',
"-//W3C//DTD XHTML 1.0 Transitional//EN",
$document->formatOutput = true;

$html = $document->documentElement;
$head = $document->createElement('head');
$title = $document->createElement('title');
$text = $document->createTextNode('Title of Page');
$body = $document->createElement('body');



This outputs: (http links removed due to spam)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd">
<html xmlns="w3org1999xhtml">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Title of Page</title>

Note the saveXML function. If saveHTML was used instead, you get the output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd">
<head><title>Title of Page</title></head>
arturm at union dot com dot pl
17 years ago
To create HTML document with doctype:

= DOMImplementation::createDocumentType("html",
"-//W3C//DTD HTML 4.01//EN",
$doc = DOMImplementation::createDocument(null, 'html', $doctype);
sleistico at gmail dot com
4 years ago
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.
To Top