doushajian2018 2015-05-22 14:58
浏览 69
已采纳

如何避免在PHP中重复实现的getter函数

While this question is about methods of solving this problem, I am particularly interested in good clean OO design and solutions that conform to current PHP best practices. Thanks in advance for taking a look.

I have the following interface:

<?php

interface Tool {

  /**
   * Return the unique name of the tool
   */
  public function getName();

}

The majority of classes that implement this interface in my project looks like this:

<?php

class Drill implements Tool {

    protected $toolName = 'drill';

    public function getName() {
        return $this->toolName;
    }

}

And so the problem is I have dozens of classes that repeat this logic, duplicating the $toolName property and getName() logic, breaking the simple rule of 'Don't repeat yourself'

One solution I have considered is the following:

<?php

abstract class BaseTool implements Tool {

    public function getName() {
        return $this->toolName;
    }

}

Then simply have tool classes extend the abstract BaseTool class:

<?php

class Drill extends BaseTool {

    protected $toolName = 'drill';
}

However this now means that I lose the ability to force implementing classes to define the function getName() if they extend the BaseTool class, which can lead to incorrect implementations.

I also think that by having the BaseTool class return $this->toolName, it is making assumptions about the implementing classes and breaks encapsulation.

I have used a simple example to demonstrate the problem but hope you get what I'm trying to solve, and that this problem may also relate to more complex situations. Appreciate your thoughts.

  • 写回答

3条回答 默认 最新

  • drl9940 2015-05-22 22:01
    关注

    How to avoid duplicating implemented getter function in PHP

    Abstract classes are often used to group duplicated code. You're on the right path. As for your doubts about the choice...

    However this now means that I lose the ability to force implementing classes to define the function getName() if they extend the BaseTool class, which can lead to incorrect implementations.

    By extending the BaseTool class, a class inherits getName() (that's the idea with defining it in the abstract class). I'm not sure why that leads to incorrect implementations or why you'd have to "force implementing classes to define it." They get it automatically by extending the abstract class.

    I also think that by having the BaseTool class return $this->toolName, it is making assumptions about the implementing classes and breaks encapsulation.

    It might be cleaner if you define the toolName in the abstract class, and you set its value in the constructor?

    <?php
    
    abstract class BaseTool implements Tool {
    
        protected $toolName;
    
        public function __construct($toolName)
        {
            $this->toolName = $toolName;
        }
    
        public function getName() {
            return $this->toolName;
        }
    
    }
    

    You define a constructor in the extended class to put its name:

    <?php
    
    class Drill extends BaseTool {
    
        public function __construct()
        {
            parent::__construct("drill");
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程