1

我有两个类,它们具有几乎相同的方法,都称为 myMethod(),但是,myChild2::myMethod() 在其中有一行额外的代码,它使用了一个局部变量。我宁愿不在两个子类中复制代码。我想我可以将 myMethod 移动到 myParent,然后添加一个 if 语句来检测哪个子类正在调用该方法,但我猜这是不对的。最干净的方法是什么?谢谢

class myParent
{
    //code...
}

class myChild1 extends myParent
{
    public function myMethod()
    {
        // A bunch of code common to both myChild1::myMethod() and myChild2::myMethod() goes here
        // A bunch more code common to both myChild1::myMethod() and myChild2::myMethod() goes here
    }
}

class myChild2 extends myParent
{
    public function myMethod()
    {
        // A bunch of code common to both myChild1::myMethod() and myChild2::myMethod() goes here
        someOtherLineOfCode($someLocalVariable); // A single unique line of code goes here which uses a local variable
        // A bunch more code common to both myChild1::myMethod() and myChild2::myMethod() goes here
    }
}
4

3 回答 3

1

你可以这样做:

class myParent
{
    public function myMethod()
    {
        // A bunch of code common to both myChild1::myMethod() and myChild2::myMethod()     goes here
        $this->templateMethod($someLocalVariable);
        // A bunch more code common to both myChild1::myMethod() and     myChild2::myMethod() goes here
    }

    protected function templateMethod($x) {}
}


class myChild2 extends myParent
{
    protected function templateMethod($x) {
        // some extra line
    }
}

取决于你到底在做什么,我认为这是一个干净的解决方案

于 2013-04-23T16:24:31.880 回答
1

我会说将它移到myParent并添加一个构造函数参数。取决于是什么someOtherLineOfCode,但您至少可以添加一个标志来确定是否执行它,如下所示:

class myParent
{
    public function __construct($shouldExecute) {
        $this->$executeSomeOtherCode = $shouldExecute;
    }

    public function myMethod()
    {
        // A bunch of code common to both myChild1::myMethod() and myChild2::myMethod() goes here

        if($this->$executeSomeOtherCode) {
            someOtherLineOfCode($someLocalVariable); // A single unique line of code goes here which uses a local variable
        }

        // A bunch more code common to both myChild1::myMethod() and myChild2::myMethod() goes here
    }

    //code...
}

class myChild1 extends myParent
{
    public function __construct()(
        parent::__construct(FALSE);
        // Do stuff specific for Bar
    }

    // Other code
}

class myChild2 extends myParent
{
    public function __construct()(
        parent::__construct(TRUE);
        // Do stuff specific for Bar
    }

    // Other code
}
于 2013-04-23T16:26:38.947 回答
1

有几个选项可以实现您的目标。您选择哪一种取决于独特线路的用途。

覆盖方法

首先,您可以myMethod()在父级中,然后在 myChild2() 中覆盖它,包括额外的行,然后调用父级。如果额外的代码行可以独立于其余部分执行,这将起作用myMethod()

class myChild2 extends myParent
{
     public function myMethod()
     {
          someOtherLineOfCode($someLocalVariable);
          parent::myMethod();
      }
}

传递标志参数

如果执行顺序很重要,您可以在运行时检测对额外功能的需求:

class Parent
{
     function myMethod($enableExtraFunctionality=false)
     {
          // Common stuff
          if ($enableExtraFunctionality) 
          {
               someOtherLineOfCode($someLocalVariable);
          }
          // More common stuff
      }
 }

然后将变量设置为仅在myChild2()

 class myChild2 extends Parent
 {
     function myMethod()
     {
          parent::myMethod(true);
     }
  }

如果您愿意,您还可以将该标志作为类变量而不是函数参数传递。

检测调用类的名称

作为先前方法的变体,您可以检测孩子的班级名称。查看get_class() PHP 手册页中的注释以获取详细信息。

于 2013-04-23T16:27:46.400 回答