0
\$\begingroup\$

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 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.)

<?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?

\$\endgroup\$
  • 2
    \$\begingroup\$ Being discussed on meta \$\endgroup\$ – rolfl Aug 17 '18 at 14:10
  • 2
    \$\begingroup\$ What's the use case for the function (or class)? How t's better than just a hyperlink written as is? \$\endgroup\$ – Your Common Sense Aug 17 '18 at 14:58
  • \$\begingroup\$ If by "as is", you mean as a string literal, the purpose of the function (and now class) was to allow links to be generated dynamically, mostly from database data. For example, if the URL of a linked reference changes, I change it in the database, and all links automatically update. \$\endgroup\$ – NewSites Aug 17 '18 at 17:11
  • 1
    \$\begingroup\$ No, the database stores the URLs, not the HTML code. It's not just the URLs that can change, but also the link text (which could be an image tag), querystring, anchors, and window names. Using the function (or method) allows all of that to be determined dynamically. \$\endgroup\$ – NewSites Aug 17 '18 at 19:38
  • 3
    \$\begingroup\$ I rolled back your last edit. After getting an answer you are not allowed to change your code anymore. This is to ensure that answers do not get invalidated and have to hit a moving target. If you have changed your code you can either post it as an answer (if it would constitute a code review) or ask a new question with your changed code (linking back to this one as reference). \$\endgroup\$ – Graipher Aug 22 '18 at 15:05
1
\$\begingroup\$

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 <a> 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.

\$\endgroup\$
  • \$\begingroup\$ "every link is generated only once": Not true. Links can appear many times in different pages, and sometimes in different places on a single page. / The "code is more intended to create an HTML <a> tag dynamically than to create an URL": Correcting what I said before, since the query and anchor are part of the URL, this is doing both. The single responsibility here is to generate the HTML tag, and the URL is one of its attributes. It is not suitable to treat the URL as a single parameter because any given webpage may need to be called with different queries and anchors. \$\endgroup\$ – NewSites Aug 20 '18 at 13:08
  • 1
    \$\begingroup\$ I answered based on the information provided in the question. \$\endgroup\$ – Your Common Sense Aug 20 '18 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.