Baigudin Software avatar
Baigudin Software logo
home
ru region
en region

PHP DOM Builder API

We have participated in OS DAY 2017 conference

The Baigudin Software Company has participated in OS DAY – a fourth scientific-practical conference, which aims to determine a mission of Russia in developing sphere of operating systems.

Optimization of source code

"The program would have been working if you had held your breath and had not been breathing". It is a good statement, but if a program is working slowly, or it is not flexible, what will we do? Yes, we need to optimize, to debug, and to test it again. Clearly, acceptable compromise between usability and performance is not being found so easily. Just, we should be thinking about it while designing time, and future updating will be easy.

Elements fetching optimization

Results of searching do not cache. This is the main rule which  you need to remember. Every time, when we inquire to find elements of tree, the library scans that tree again. Therefore, the searching is faster if a branch is shorter.

The best way is to save a searching result to local variable:

<?php
// It is bad
$node $document->find('a')->addClass('link');
$node->attr('href''baigudin.software');
// It is good
$document->find('a')->addClass('link');
$document->find('a')->attr('href''baigudin.software');
?>

We can use a chain of calls:

<?php
// It is good too
$document->find('a')->addClass('link')->attr('href''baigudin.software');
?>

We can truncate a tree to "div.article":

<?php
// It is bad
$document->find('div.article h2');
$document->find('div.article a');
// It is good
$node $document->find('div.article');
$node->find('h2');
$node->find('a');
?>

Indexing of elements

An object of ElementNode class can get a string index, which is called a key, by using key method of the library. The key will be associated with a sequence number of the element in new created list of elements. Thus, we can get an access to the element of a list by its key.

<?php
use DomBuilder\Element as Element;
// Create document
$document Element::create()
  ->
insert('html')
    ->
insert('head')
      ->
after('body')->root();
// Create empty list
$list Element::newList();
// Add elements to list
$list->pushElement::create('h1')->html('My header')->key('header') );
$list->pushElement::create('p')->html('My text') )->key('text');
// Add elements of list to document by its keys
$document->find('body')
  ->
insert$list->get('header') )
  ->
after$list->get('text') );
// Output document
echo Element::getDocument($document);
?>

We want to mark that you can add a key for an element when you have created it, or you can add a key for a list. If you call key method for ElementList class, the key will be added for last added element to the list.

Note: The library does not monitor that a key is one and only. It is needed in keys only for associating an element in elements list. Therefore, if a list has greater than one element with equal keys, it returns the last added element with that key to its.

We have considered a few simple ways to make applications faster and more intelligible for future supporting. In next article, we will continue to talk about optimizations and tell you about multi-language supporting in range of the library. 

To the previous article

To the articles list

To the next article