【问题标题】:PHP: Object for HTML hyperlinkPHP:HTML 超链接的对象
【发布时间】:2018-08-09 18:38:00
【问题描述】:

更新:我为该类编辑了下面的代码,以更正运行它时出现的错误(需要“新”来创建对象)。通过该更正,代码运行并正确生成 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 的最佳方式吗?

【问题讨论】:

  • 这里的逻辑看起来不错,但我建议不要使用像sLocn 这样的快捷方法名称。另一方面,代码格式...
  • 这在CodeReview 上可能会更好。

标签: php object


【解决方案1】:

如果s 前缀表示它是一个字符串变量,您可能想知道,从 PHP/7 开始,您可以使用 declare scalar arguments type 并且在所有版本中,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;
    }

}

【讨论】:

  • 谢谢。根据您的建议,我现在已经在我的所有 PHP 文件中实现了严格输入。至于格式,我确实觉得这样更容易阅读,而且我已经很老了。我使用 Notepad++,而不是 IDE。代码只由我维护,所以我关心功能和效率,但我更喜欢使用我觉得合适的格式。我希望它不会使您难以审查。我已经为这个问题写了一个附录。你能告诉我你对这种变化的看法吗?
猜你喜欢
  • 2012-10-17
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 2013-06-21
  • 2012-07-20
  • 2019-05-17
  • 1970-01-01
  • 2015-06-16
相关资源
最近更新 更多