HiveBrain v1.2.0
Get Started
← Back to all entries
principlephpMinor

OOP approach to using DOMDocument

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
usingapproachoopdomdocument

Problem

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.

setPage('template.html');
//find body
$body= $v->returnBody();
//create new element and attach it to body
$v->newElement('div', $body, "id=container");

$cont = $v->getElement("container");

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

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

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

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

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


Where the Viewer class is:

```
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->dom->createElement($type, $content);
if(gettype($params) == "string" && strlen($params) > 0){
$attrCollec = preg_split("/;/", $params);
for

Solution

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 ?> at the end of the file. This helps with line break and whitespace issues.



I'll continue editing as I explore more.

Context

StackExchange Code Review Q#26947, answer score: 5

Revisions (0)

No revisions yet.