如何强制实施受保护的静态功能 [英] How to force an implementation of a protected static function

查看:134
本文介绍了如何强制实施受保护的静态功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个抽象类(或接口),以强制扩展类实现受保护的静态函数。
但这对于抽象类和接口都是不可能的。

I'm trying to write an abstract class (or interface) which forces the extending class to implement a protected static function. But this is neither possible with an abstract class nor an interface.

错误:


  • 静态函数不应该是抽象的

  • 必须省略接口成员的访问类型

任何想法如何实现?

更新

目的基本上是静态地调用public函数。这样,无需实例化该类。
也不必从类外部代码中调用_doSpecificStuff()。

The purpose is basically to call the public function statically. This way the class does not need to be instanciated. It is also not necessary to make _doSpecificStuff() callable from class-external code.

abstract class Foo
{
    public static function doStuff()
    {
        [generic code] 

        static::_doSpecificStuff();
    }

    // sth like that would be nice to have:
    abstract static protected function _doSpecificStuff();
}


推荐答案

从理论上从实际的角度来看,并不需要真正声明一个静态方法摘要。

From a theoretical as well as a practical standpoint, there's no real need to declare a static method abstract.

抽象方法中有一个子类可以填空。这通常是因为父类(原始抽象类)执行了一些通用操作,但可以/必须适应某些特定情况,因此可以迫使子类仅在其他通用算法中实现此特定变量部分。在更大的算法中,抽象方法应该是一个空白点。

Abstract methods are there to have a child class fill in a blank. That's typically because the parent class (the original abstract class) does some generic operation, but can/must be adapted to certain specific situations and can thus force child classes to implement only this particular variable part in the otherwise generic algorithm. Abstract methods are supposed to be a blank spot within a larger algorithm.

父方法会在不知道或关心的情况下调用其抽象方法的实现实现它们或如何实现

A parent method would call implementations of its abstract methods without knowing or caring who implements them or how they're implemented:

abstract class Foo {

    public function someAlgo() {
        $foo = $this->doSomethingSpecific();
        ...
    }

    abstract protected function doSomethingSpecific();

}

Foo 不在乎空白的 doSomethingSpecific 是谁或什么,它只是依赖于它的存在及其签名,而抽象强制执行。实现此方法或其实现方式的特定对象是变量。这很重要,并且是问题的核心。

Foo doesn't care who or what fills in the blank doSomethingSpecific, it just relies on it being there and its signature, which abstract enforces. The specific object which implements this method or how it implements it is variable. This is important and is at the core of the issue.

在这种情况下,声明静态方法摘要几乎没有用。任何静态方法都可以像非静态方法一样实现,因此这里没有用。如果应该将类本身作为抽象方法作为较大的通用算法的一部分进行调用,如上所述,则无需使用静态方法。

Declaring a static method abstract is pretty useless in this scenario. Any static method can just as well be implemented as a non-static method, so there's no use for it here. If the class itself is supposed to call the abstract method as part of a larger generic algorithm as described above, there's no need for a static method.

因此剩下的唯一情况是静态方法适用于公共静态方法,该方法可从任何地方调用:

So the only scenario left for a static method is for a public static method which is callable from anywhere:

abstract class Foo {

    abstract public static function bar();

}

class Baz extends Foo {

    public static function bar() { ... }

}

Baz::bar();

问题是,因为抽象类Foo 本身并不是在调用此函数,而是仅从外部代码调用此函数,您并不是在真正地谈论填充空白方法,而是在谈论界面。因此,您应该改用界面

但即使在那儿,由于您必须在源代码中键入特定的类名,并进行硬编码,

The thing is, since the abstract class Foo is not itself calling this function but this function is only called from external code, you're not really talking about a fill-in-the-blank method, you're talking about an interface. So, you should be using an interface instead.
But even there, since you have to type the specific class name in your source code, hardcoded, there's little point for an interface as well.

声明接口或抽象方法签名的目的是要修复方法签名,以便任何代码都可以调用该特定签名。方法,而无需关心它特别调用了什么对象。但是,由于您必须对类名进行硬编码,因此调用它的对象没有变化。如果您键入 Baz :: bar(),则您确切知道要在哪个类上调用哪种方法。因此,抽象接口/签名毫无意义。

The point of declaring an interface or abstract method signature is that you want to fix the method signature so any code can call that particular method without caring what object it's calling it on in particular. But since you have to hardcode the class name, there's no variability in the object you're calling it on. If you type Baz::bar(), you know exactly what class you're calling what method on. Therefore there's little point in abstracting the interface/signature.

比较:

interface FooInterface {

    public function bar();

}

function baz(FooInterface $foo) {
    $foo->bar();
}

函数 baz 由于接口声明,可以依靠具有 bar()方法的参数。实现该方法的特定对象无关紧要,并且会有所不同。

The function baz can rely on its argument having a bar() method due to the interface declaration. The specific object that's implementing the method is irrelevant and will vary.

abstract class Foo {

    public function someAlgo() {
        $foo = $this->doSomethingSpecific();
        ...
    }

    abstract protected function doSomethingSpecific();

}

Foo 可以使用 doSomethingSpecific 方法依靠它。实现该方法的特定对象无关紧要,并且会有所不同。

The class Foo can rely on it having the doSomethingSpecific method. The specific object that's implementing the method is irrelevant and will vary.

abstract class Foo {

    abstract public static function bar();

}

class Baz extends Foo {

    public static function bar() { ... }

}

Baz::bar();

您在这里究竟是依赖还是抽象的?您可以很确定地确保 Baz 每次都会具有方法 bar(),因为您只会调用它在相同的硬编码类上。这里什么都没有变。

What exactly are you relying on or abstracting here? You can be pretty darn sure Baz will have the method bar() every time, because you're only ever calling it on the same hardcoded class. Nothing is variable here.

这篇关于如何强制实施受保护的静态功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆