I have a function that generates HTML for hyperlinks. It's arguments may be string literals or variables read from the database. An example call to this function looks like:
hLink('http://example.com', 'website', 'section', 'input=0', '_blank');
which returns:
'<a HRef="http://example.com?input=0#section" target="_blank">website</a>'
The first argument is required, and the remaining four have defaults.
In my first foray into object-oriented programming, I've written a replacement for this function by a method defined in a class, so the function calls are replaced with calls to that method acting on an object from that class. I've written two versions:
In the first version, each link works by creating a new instance of the class, with the required arguments of
hLink()
handled by the class constructor, and optional arguments handled by setting properties of the object, and then an output methodhLink()
finally acting on those properties to generate the HTML link. If you want to see the code for this version, it's in the edit history of this question, but I've removed it in favor of the second version, which I think is probably better.In the second version, the class has no constructor. One instance of the class is created and all links are generated by this one object. The class has a method
clear()
that resets all the properties to empty, as well as additional methods to set the values of any properties that are needed, and finally an output methodhLink()
to generate the HTML link.
I wrote the second version because it seemed that the first was wasteful by creating an object for each link, and those objects were never used again after generating the HTML. So the second version reuses the same object repeatedly for this task.
The code for the second version is as follows: (Pardon my formatting. I know it's unconventional, but I find this easier to read.)
<?php declare(strict_types = 1);
$oLink = new link;
class link
{private $sURL;
private $sText;
private $sAnchor;
private $sQuery;
private $sWindow;
public function clear()
{$this->sURL = '';
$this->sText = '';
$this->sAnchor = '';
$this->sQuery = '';
$this->sWindow = '';
return $this;
}
public function URL(string $sURL) {$this->sURL = $sURL; return $this;}
public function text(string $sText) {$this->sText = $sText; return $this;}
public function anchor(string $sAnchor) {$this->sAnchor = $sAnchor; return $this;}
public function query(string $sQuery) {$this->sQuery = $sQuery; return $this;}
public function window(string $sWindow) {$this->sWindow = $sWindow; return $this;}
public function hLink()
{return('<a HRef="' . $this->sURL .
($this->sQuery == '' ? '' : '?' . $this->sQuery) .
($this->sAnchor == '' ? '' : '#' . $this->sAnchor) .
'"' . ($this->sWindow == '' ? '' : ' target="' . $this->sWindow . '"') .
'>' . $this->sText . '</a>');
}
}
echo('<p>Link to ' . $oLink->clear()->URL('http://example.com')->text('website')->anchor('section')->query('input=0')->window('_blank')->hLink() . ' in text</p>');
I've tested the above code as a freestanding script, and it does output the intended:
<p>Link to <a HRef="http://example.com?input=0#section" target="_blank">website</a> in text</p>
I'd like to know if I'm doing this right, or even if I should be doing this at all (i.e., if this is a sensible use of objects). Is this the best way to generate HTML for hyperlinks embedded in text in PHP?