Skip to content

Using DOM in PHP

Greg Bowler edited this page Jun 7, 2017 · 17 revisions

The easiest way to use DOM in your PHP application is to install it as a dependency via Composer.

From your project's root directory, assuming you have Composer installed:

composer require phpgt/dom

Composer will download and install DOM and its dependencies into your project within the vendor/ directory. Inside that directory is the Composer autoloader script, which can be used to magically load any class you have installed.

Once DOM is installed using Composer:

<?php
require "vendor/autoloader.php";
$document = new Gt\Dom\HTMLDocument("<!doctype html><h1>Hello, World!</h1>");
$h2 = $document->createElement("h2");
$h2->textContent = "Goodbye, World!";
$document->body->appendChild($h2);

echo $document->saveHTML();

Without Composer

All releases are available in zip and tar format for downloading. Of course you can clone the git repository too. Once you have this library's PHP scripts in your project you can use a simple autoloader function to load the classes as they are referenced in your code. This library follows the PSR-4 standard for autoloading, so a simple function can be used as follows:

// TODO: Document simple PSR-4 autoloader.

Clone this wiki locally