抽象类扩展php中的抽象类?

发布于 2024-08-31 21:12:23 字数 239 浏览 5 评论 0原文

我正在研究一个简单的抽象数据库类。在我使用这个类时,我希望有一些实例是单例。我正在考虑有一个不是单例的抽象类,然后将其扩展为另一个单例的抽象类。这可能吗?受到推崇的?


编辑:我想要两个几乎相同的摘要,除了一个是单例。因此,唯一的区别是,一个将具有另一个的所有功能,但将具有其他属性和方法,使其表现得像单例。

我希望为此拥有一个基类代码库,这样当我进行更改时,我不必保持两个文件同步。

I am working on a simple abstract database class. In my usage of this class, I'll want to have some instance be a singleton. I was thinking of having a abstract class that is not a singleton, and then extend it into another abstract class that is a singleton. Is this possible? Recommended?


Edit: I want to have two abstract that are practically identical, except one is a singleton. So the only difference will be that one will have all the functions of the other, but will have the other properties and methods that make it behave like a singleton.

I'd like to have one base class code base for this so as I make changes, I don't have to keep two files in sync.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏舞 2024-09-07 21:12:23

在我做事的方式中,我相信抽象的单例是没有用的。这是因为,

1)您想要成为单例的是您实例化以在应用程序中使用的最终类,无论它是库、模型、控制器还是视图,而不是抽象。

2)添加单例方法很简单,8行就可以写完。见下文。

protected static $_instance;
public static function getInstance()
{
    if (!isset(self::$_instance)) {
        self::$_instance = new self();
    }
    self::$_instance;
}

3) PHP 5.3以下版本不支持后期静态绑定。这将导致实例化抽象类而不是继承它的最终类,并且不会按预期运行,正如 Gordon 和 nuqqsa 已经提到的那样。因此,为了向后兼容,最好避免它。

In the way that I do things, I believe that there's no use for an abstract singleton. This is because,

1) What you want to be a singleton is the final class you instantiate for use within the application whether it'd be a library, model, controller or view and NOT the abstract.

2) Adding the singleton method is easy and can be written in 8 lines. See below.

protected static $_instance;
public static function getInstance()
{
    if (!isset(self::$_instance)) {
        self::$_instance = new self();
    }
    self::$_instance;
}

3) PHP 5.3 below version doesn't support late static binding. This will result in instantiating the abstract class instead of the final class inheriting it and will not function as expected, as already mentioned by Gordon and nuqqsa. So, for backward compatibility, better avoid it.

黯然#的苍凉 2024-09-07 21:12:23

单例模式的实现必须满足两个要求:

  1. 它必须提供一种机制来访问单例类实例而不创建类对象
  2. 它必须持久化单例对象,以便它不会被实例化多次

只要提供了这一点,变体就可以了是多个。如果您需要的话,使类抽象从另一个抽象扩展并没有什么问题。但是,正如 @Gordon 所说,请注意,重写静态方法/属性会导致 PHP << 中的特殊行为。 5.3.

The implementation of the singleton pattern must satisfy two requirements:

  1. It must provide a mechanism to access the singleton class instance without creating a class object
  2. It must persist the singleton object so that it is not instantiated more than once

As long as that's provided, the variations are multiple. There's nothing wrong with making the class abstract extending from another abstract, if that's what you need. BUT, as @Gordon says, be aware that overriding static methods/properties causes peculiar behaviours in PHP < 5.3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文