0

I have 2 functions, they currently are doing the same thing.. In the future, it's quite possible that I need to add more functionality to one, but at the moment they act the same.

private function GetAnswerId( $value, $fieldId )
{
    // Code goes here, returns something      
}

private function GetQuestionId( $value, $fieldId ); // Currently same code as GetAnswerId... But might change later!

Is there some clever way of communicating to future developers that okay this function is currently the same implementation but in the future it won't be.. I don't want to just copy the code in GetAnswerId because that's drydicoulous. but I also don't want to use the same function because that's not forward thinking.

Virtual? Abstract? something like that :S

4

3 回答 3

1

将您的重复功能移至基类:

class BaseClass
{
    protected function getId($value, $fieldId) {
        ...
    }
}

使用一种方法getId()代替GetAnswerId()and GetQuestionId()。稍后,如果您决定更改问题 ID 的功能,只需将新方法添加到当前类:

class CurrentClass extend BaseClass
{
    protected function GetQuestionId($value, $fieldId) {
        ...
    }
}
于 2013-10-31T14:09:37.747 回答
1

在第二个函数中调用第一个函数并从那里扩展:

private function GetQuestionId( $value, $fieldId )
{
     GetAnswerId( $value, $fieldId );

     // extra code
}
于 2013-10-31T13:58:31.753 回答
0

在您的情况下,我会在您的函数中添加一些文档,说明它计划很快改变其行为并进行此函数调用并返回GetAnswerId的值。

private function GetQuestionId($value, $fieldId)
{
    return $this->GetAnswerId($value, $fieldId);
}
于 2013-10-31T13:57:21.417 回答