PHP:HTML 超链接的对象



>更新:我为该类编辑了下面的代码,以纠正运行它时出现的错误(需要"new"来创建对象(。通过该更正,代码将运行并正确生成 HTML。但由于我是OOP的新手,我仍然想知道我是否采取了正确的方法。

第二次更新:我在下面的附录中解决了对这种技术效率的担忧。我还更正了 URL 中锚点名称和查询字符串的顺序,这些顺序由 hLink(( 正确处理。


我有一个为超链接生成 HTML 的功能。它的参数可能是从数据库中读取的字符串文字或变量。对此函数的示例调用如下所示:

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

返回:

'<a HRef="http://example.com?input=0#section" target="_blank">website</a>'

第一个参数是必需的,其余四个参数具有默认值。

我正在考虑用类中定义的方法替换此函数,因此函数调用将替换为对该方法的调用,该方法对类构造函数的调用。我是面向对象编程的新手,所以我想知道我是否做对了,或者我是否应该这样做。

我想出的类是:

class link
{private $sURL;
private $sText   = '';
private $sLocn   = '';
private $sQuery  = '';
private $sWindow = '';
public function __construct($sURL)
{$this->sURL = $sURL;}
public function sText($sText)     {$this->sText   = $sText;   return $this;}
public function sLocn($sLocn)     {$this->sLocn   = $sLocn;   return $this;}
public function sQuery($sQuery)   {$this->sQuery  = $sQuery;  return $this;}
public function sWindow($sWindow) {$this->sWindow = $sWindow; return $this;}
public function hLink()
{...}
}

其中方法 hLink(( 的主体与原始函数 hLink(( 的主体相同。

对于此类,上述函数调用的等效项将是:

(new link('http://example.com'))->sText('website')->sLocn('section')->sQuery('input=0')->sWindow('_blank')->hLink()

旨在生成与以前相同的字符串:

'<a HRef="http://example.com?input=0#section" target="_blank">website</a>'

这看起来如何?我走在正确的轨道上吗?


补遗

令我困扰的是,上述技术为页面上的每个链接创建一个新对象,并且该对象在使用一次为链接创建HTML后没有任何作用。这是一个小对象,一页上不会超过几百个,所以它不会是一个很大的内存负担,但它仍然看起来浪费和低效。我有一个解决这个问题的想法:

  • 在类中,没有构造函数,并且有一个方法clear((,该方法将所有属性的值设置为默认值(空(。
  • 创建此类的一个实例。
  • 使用此对象在页面上放置一个链接,在其上调用 clear(( 方法,然后调用设置所需的任何属性值的方法,最后像以前一样调用输出方法 hLink((。

其代码如下:(请原谅我的格式。我知道这是非常规的,但我发现这更容易阅读。

<?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('Link to ' . $oLink->clear()->URL('http://example.com')->text('website')->anchor('section')->query('input=0')->window('_blank')->hLink() . ' in text');

我已经将上面的代码作为一个独立的脚本进行了测试,它确实输出了预期的:

Link to <a HRef="http://example.com?input=0#section" target="_blank">website</a> in text

那么这是更好的方法吗?现在,每次页面上需要另一个链接时,只有一个对象会被重用。这是为 PHP 中嵌入文本中的超链接生成 HTML 的最佳方式吗?

如果s前缀旨在指示它是一个字符串变量,您可能想知道,从 PHP/7 开始,您可以声明标量参数类型,并且在所有版本中,docblocks 是体面的 IDE 认可的标准工具。

代码格式也相当不寻常。最后,试图使你的代码紧凑以更好地利用屏幕空间并没有真正的回报:它对眼睛更难(相信我,你会变老(,而且没有其他人这样做,所以你不能使用编辑器的代码格式功能而不投入大量时间重新配置它。

除此之外,我看不出有什么本质上的错误。

下面是一个带有类型提示、文档块和 PSR-2 的基本相同代码的快速示例:

class Link
{
/**
* @var string
*/
private $url;
/**
* @var string
*/
private $text = '';
/**
* @param string $url
*/
public function __construct(string $url)
{
$this->url = $url;
}
/**
* @param string $text
* @return Link
*/
public function text(string $text): Link
{
$this->text = $text;
return $this;
}
}

最新更新