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

查看:196
本文介绍了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;

它会被认为是好的风格,简单地声明这个类是抽象的吗?如果没有,与抽象类相比,将构造函数隐藏为protected方法有什么优点?

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.

更新: / strong>首先,感谢所有的答案!但是许多答案听起来很相似:你不能实例化抽象类,但是对于注册表,为什么不使用单例模式?

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?'

不幸的是,完全是我的问题的重复。使用单例模式(aka隐藏 __ construct())相比只是声明 abstract code>而不必担心? (例如,它是开发人员之间的强烈内涵,抽象类并不实际使用。)

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 作为私有方法


    • ,所以该类不能从外部实现

    • That defines __construct and __clone as private methods
      • so the class cannot be instanciated from outside



      现在,为什么要使用Singleton,而不仅仅是静态方法?我想,至少有几个原因可以有效:


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


      • 使用单例意味着使用类的实例;使得更容易将非单例类转换为单例类:只需要使 __ construct __ clone private,并添加一些 getInstance 方法。

      • 使用单例也意味着你可以访问一个正常的实例: $ this ,属性,...

      • 哦,第三个(不确定,但可能有其重要性) :with 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());
          

          会工作...但它不觉得很自然...这是一个非常简单示例(参见我早些时候用Late Static Binding和魔法方法说的)

          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天全站免登陆