简体   繁体   English

PHP抽象静态类方法的替代模型

[英]Alternative model for PHP abstract static class methods

Ok, so I've read lots of posts on here about this, but I think this is a specific and new question. 好的,所以我在这里阅读了很多有关此的文章 ,但是我认为这是一个特定的新问题。

I want the functionality of what lots of us on here wrongly call an "abstract static method". 我想要我们这里很多人错误地称为“抽象静态方法”的功能。 That is: I want a class that insists classes that extend it implement certain static methods . 也就是说: 我想要一个类,它坚持要对其进行扩展的类来实现某些静态方法

It seems there are two roads round the much discussed problem of not allowing abstract static, but both seem inelegant. 围绕被广泛讨论的不允许抽象静态存在的问题,似乎有两条路可走,但两条路线似乎都很微不足道。

Problem 问题

The following generates an error: "PHP Strict Standards: Static function A::fn() should not be abstract". 以下生成错误:“ PHP Strict Standards:静态函数A :: fn()不应为抽象”。

<?php
abstract class A
{
     abstract static public function fn();
}

class B extends A
{
    static public function fn()
    {
         // does something on class vars etc.
    }
}

Solution 1: use Interfaces 解决方案1:使用接口

<?php
interface iA {
    static function fn();
}


abstract class A implements iA 
{
} // obviously this would have other stuff in it in reality

class B extends A
{
    static public function fn()
    {
         // does something on class vars etc.
    }
}

The problem with this is that an interface assumes (dictates) that all the methods be public. 问题在于接口假设(指示)所有方法都是公共的。 So while this achieves the goal of insisting that sub-classes of an abstract class have certain static methods, it can only be used for public methods; 因此,尽管达到了坚持抽象类的子类具有某些静态方法的目的,但它只能用于公共方法; there is still no way to ensure subclasses of A implement a protected static method. 仍然无法确保A的子类实现受保护的静态方法。

Protected static methods are useful when the method works entirely on static data but ought not to be called from 'outside'. 当静态方法完全适用于静态数据但不应从“外部”调用时,受保护的静态方法很有用。

Solution 2: exceptions 解决方案2:例外

abstract class A
{
     static public function fn()
     {  
         throw new Exception(get_called_class() . " must implement fn()");
     }

}

class B extends A
{
    static public function fn()
    {
         // does something on class vars etc.
    }
}

This works at runtime if fn() when called. 如果在调用fn()时可以在运行时使用。 But it's not useful to have to deal with coding syntax mistakes at runtime. 但是在运行时必须处理编码语法错误并没有什么用。 And while this could be a hack for one method, it's a lot more code for each method. 尽管这可能只是一种方法的破解,但每种方法都有很多代码。

Is there something so wrong with this design that the language rightly forbids it? 这种设计有什么问题,以至于该语言正确地禁止了这种设计? Seems odd that there are various ways to insist subclasses provide most types of method, but not static ones. 奇怪的是,有多种方法可以坚持要求子类提供大多数类型的方法,但不能提供静态方法。

It's because static methods belong to the class where they are defined. 这是因为静态方法属于定义它们的 A::fn() and B::fn() are two different methods, regardless of the inheritance tree. 不管继承树如何, A::fn()B::fn()是两种不同的方法。 So it makes little sense to define A::fn() as abstract . 因此,将A::fn()定义为abstract几乎没有意义。

Maybe your methods in question should not be abstract at all? 也许您所讨论的方法根本不应该是抽象的? If you want to use any kind of polymorphism, there should be objects involved. 如果要使用任何种类的多态性,都应该涉及对象。

I might be totally wrong, but I have no problem in using 我可能完全错了,但是我使用时没有问题

abstract class A
{
     abstract static public function fn();
}

class B extends A
{
    static public function fn()
    {
         print 1;
    }
}

B::fn();

It produces 1 as an output. 它产生1作为输出。 I have run your code in here . 我已经在这里运行您的代码。 What version of php do u use ? 您使用什么版本的php?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM