Object for generating HTML for hyperlinks 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:

'&lt;a HRef="http://example.com?input=0#section" target="_blank"&gt;website&lt;/a&gt;'


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 method hLink() 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 method hLink() 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.)

&lt;?php declare(strict_types = 1);
$oLink = new link;
class link
    {private $sURL;
     private $sText;
     private $sAnchor;
     private $sQuery;
     private $sWindow;
     public function clear() 
        {$this-&gt;sURL    = ''; 
         $this-&gt;sText   = ''; 
         $this-&gt;sAnchor = '';
         $this-&gt;sQuery  = ''; 
         $this-&gt;sWindow = ''; 
         return $this;
         }
     public function URL(string $sURL)       {$this-&gt;sURL    = $sURL;    return $this;}
     public function text(string $sText)     {$this-&gt;sText   = $sText;   return $this;}
     public function anchor(string $sAnchor) {$this-&gt;sAnchor = $sAnchor; return $this;}
     public function query(string $sQuery)   {$this-&gt;sQuery  = $sQuery;  return $this;}
     public function window(string $sWindow) {$this-&gt;sWindow = $sWindow; return $this;}
     public function hLink()
        {return('&lt;a HRef="' .  $this-&gt;sURL . 
                              ($this-&gt;sQuery  == '' ? '' : '?' . $this-&gt;sQuery) . 
                              ($this-&gt;sAnchor == '' ? '' : '#' . $this-&gt;sAnchor) .
                        '"' . ($this-&gt;sWindow == '' ? '' : ' target="' . $this-&gt;sWindow . '"') .
                 '&gt;' . $this-&gt;sText . '&lt;/a&gt;');
         }
     }
echo('&lt;p&gt;Link to ' . $oLink-&gt;clear()-&gt;URL('http://example.com')-&gt;text('website')-&gt;anchor('section')-&gt;query('input=0')-&gt;window('_blank')-&gt;hLink() . ' in text&lt;/p&gt;');


I've tested the above code as a freestanding script, and it does output the intended:

&lt;p&gt;Link to &lt;a HRef="http://example.com?input=0#section" target="_blank"&gt;website&lt;/a&gt; in text&lt;/p&gt;


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?
For the suggested use case having an object is an overkill.

As far as I can tell from the code provided, every link is generated only once, which makes class variables; a dedicated method to clean the state; and a code to instantiate an object all unnecessary. So a function would be more appropriate here. 

Besides, as it seems that a code is more intended to create an HTML &lt;a&gt; tag dynamically than to create an URL, and also in order to follow the Single responsibility principle, I would change the list of parameters to just 

hLink('http://example.com/?input=0#section', 'website', '_blank'); 


which will make parameters more sensible and consistent.