PHP中的静态类通过抽象关键字? [英] Static classes in PHP via abstract keyword?

查看:27
本文介绍了PHP中的静态类通过抽象关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据PHP手册,有这样一个类:

abstract class Example {}

无法实例化.如果我需要一个没有实例的类,例如对于注册表模式:

cannot be instantiated. If I need a class without instance, e.g. for a registry pattern:

class Registry {}
// and later:
echo Registry::$someValue;

简单地将类声明为抽象类是否被认为是好的风格?如果不是,与抽象类相比,将构造函数隐藏为受保护方法有什么优势?

would it be considered good style to simply declare the class as abstract? If not, what are the advantages of hiding the constructor as protected method compared to an abstract class?

提问的理由:据我所知,这可能有点滥用功能,因为手册将抽象类更多地称为具有实例化可能性的后续类的蓝图.

Rationale for asking: As far as I see it, it could a bit of feature abuse, since the manual refers to abstract classes more as like blueprints for later classes with instantiation possibility.

更新:首先,感谢大家的回答!但许多答案听起来很相似:您不能实例化抽象类,但对于注册表,为什么不使用单例模式?"

Update: First of all, thanks for all the answers! But many answers sound quite alike: 'You cannot instantiate an abstract class, but for a registry, why not using a singleton pattern?'

不幸的是,这或多或少重复了我的问题.使用单例模式(又名隐藏 __construct())与仅声明它abstract 相比有什么优势而不必担心这一点?(例如,开发人员之间有很强的内涵,abstract 类实际上并没有被使用左右.)

Unfortunately, that was more or less exactly a repeat of my question. What is the advantage of using a singleton pattern (a.k.a. hiding __construct()) compared to just declaring it abstract and not having to worry about that? (Like, e.g., it is a strong connotation between developers, that abstract classes are not actually used or so.)

推荐答案

如果您的类不打算定义某些超类型,则不应将其声明为 abstract,我会说.

If your class is not meant to define some super-type, it should not be declared as abstract, I'd say.

在你的情况下,我宁愿去上课:

In your case, I would rather go with a class :

  • __construct__clone 定义为私有方法
    • 因此无法从外部实例化该类


    现在,为什么要使用单例,而不仅仅是静态方法?我想,至少有几个理由是有效的:


    Now, why use a Singleton, and not only static methods ? I suppose that, at least a couple of reasons can be valid :

    • 使用单例意味着使用类的实例;可以更轻松地将非单例类转换为单例类:只需将 __construct__clone 设为私有,并添加一些 getInstance 方法.
    • 使用单例还意味着您可以访问可以与普通实例一起使用的所有内容:$this、properties、...
    • 哦,第三个(不确定,但可能很重要):使用 PHP <5.3,静态方法/数据的可能性较小:
      • Using a singleton means using an instance of the class ; makes it easier to transform a non-singleton class to a singleton one : only have to make __construct and __clone private, and add some getInstance method.
      • Using a singleton also means you have access to everything you can use with a normal instance : $this, properties, ...
      • Oh, a third one (not sure about that, but might have its importance) : with PHP < 5.3, you have less possibilities with static methods/data :
        • __callStatic has only been introduced in PHP 5.3
        • There is no __getStatic, __setStatic, ...
        • Same for a couple of other Magic methods !


        话虽如此,是的,一些代码如下:


        This being said, yes, some code like this :

        abstract class MyClass {
            protected static $data;
            public static function setA($a) {
                self::$data['a'] = $a;
            }
            public static function getA() {
                return self::$data['a'];
            }
        }
        
        MyClass::setA(20);
        var_dump(MyClass::getA());
        

        会起作用...但感觉不太自然...这是一个非常简单的例子(参见我之前所说的后期静态绑定和魔术方法).

        Will work... But it doesn't feel quite natural... and this is a very simple example (see what I said earlier with Late Static Binding, and magic methods).

        这篇关于PHP中的静态类通过抽象关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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