抽象类在php中扩展了抽象类? [英] abstract class extends abstract class in php?

查看:84
本文介绍了抽象类在php中扩展了抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

推荐答案

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

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

1)您想要成为单例的是您实例化用于应用程序中的最终类,无论它是库,模型,控制器还是视图,而不是摘要.

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)添加单例方法很容易,可以用8行编写.见下文.

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以下版本不支持后期静态绑定.正如Gordon和nuqqsa所提到的,这将导致实例化抽象类,而不是最终类继承该抽象类,并且将无法按预期运行.因此,为了向后兼容,最好避免使用它.

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.

这篇关于抽象类在php中扩展了抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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