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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效