OOP approach to using DOMDocument I've tried to write an "OOP" approach to printing a website using the DOMDocument class. I'm kind of stuck here because I feel like I am blundering in how I am passing and using the DOMDocument. I found that I can just return the object from the class and use it that way. 

What are some safe practices here? Best approach? I know it's far from OOP, but I at least want to be considerate of the model while learning. I realize my approach is probably all backwards.  How bad is this code and what are some ways to improve this?

All styling is done in CSS, any and all critique is welcome about any aspect of the code. 

If this isn't considered to be a useful question let me know and I will remove it.

&lt;?php
require 'viewer.php'
//new viewer class
$v= new viewer();
//read html document structure from a template
$page= $v-&gt;setPage('template.html');
//find body
$body= $v-&gt;returnBody();
//create new element and attach it to body
$v-&gt;newElement('div', $body, "id=container");

$cont = $v-&gt;getElement("container");

$v-&gt;newElement('div', $cont, "id=title", "My Website");
$v-&gt;newElement('ul', $cont, "id=menu;class=menu");
$v-&gt;newElement('li', $v-&gt;getElement("menu"), NULL, "Forums" );

$v-&gt;newElement('div', $cont, "id=content");
$v-&gt;newElement('div', $v-&gt;getElement("content"), "id=content_title", "Content Title Here!");
$v-&gt;newElement('div', $v-&gt;getElement("content"), "id=content_box");

$mb = $v-&gt;getElement("content_box");
$v-&gt;newElement('a',$mb,NULL,"Some Link");

$content = " And after the link, this is the content.";

$dd=$v-&gt;rDOM();
$c=$dd-&gt;createTextNode($content);
$mb-&gt;appendChild($c);
$v-&gt;printHTML();
?&gt;


Where the Viewer class is:

&lt;?php
class viewer {
    protected $dom;

    public function __construct(){
        $this-&gt;dom = new DOMDocument();
    } 

    //Creates an element of the type passed in string, appends parameters passed in string
    //and finally inserts into the passed node
    public function newElement($type, $insertInto = NULL, $params=NULL, $content=""){
        $element = $this-&gt;dom-&gt;createElement($type, $content);
        if(gettype($params) == "string" &amp;&amp; strlen($params) &gt; 0){
            $attrCollec = preg_split("/;/", $params);
            foreach($attrCollec as $attr){
                $value = preg_split("/=/", $attr);
                $element-&gt;setAttribute($value[0], $value[1]);
            }
        }
        $insertInto-&gt;appendChild($element);
    }
    public function getElement($param){
        //if($this-&gt;dom-&gt;validate()){
            $elem = $this-&gt;dom-&gt;getElementById($param);

            return $elem;
        //}
    }
    public function setPage($fileName){
        //does this even work? should validate before each getElement call?
        $this-&gt;dom-&gt;validateOnParse = true;
        $this-&gt;dom-&gt;loadHTMLFile($fileName);            
    }        
    public function printHTML(){

        echo $this-&gt;dom-&gt;saveHTML();
    }
    public function returnBody(){
        $nodeList = $this-&gt;dom-&gt;getElementsByTagName('body');
        $body = $nodeList-&gt;item(0);

        return $body;
    }
    public function rDOM(){
        return $this-&gt;dom;
    }
}
?&gt;
Some things I spot on the fly:


Lack of documentation. PHPDoc works well, every method, every class, every file should have a DocBlock.
Naming conventions. It's considered good naming convention to have methodNames() and $variableNames in camelCase, but ClassNames and ObjectNames in UppercaseCamelCase.
Code style. Conforming to a single code style is great. Conforming to an established standard is awesome!. Check out the PSR Code style standards page.
On the newElement method, preg_split is redundant. explode by ; or = is enough, and is faster!
Last but not least, if the entire file is PHP (without embedding HTML), it's considered safe (and even recommended!) to omit the closing ?&gt; at the end of the file. This helps with line break and whitespace issues.


I'll continue editing as I explore more.