PHP:防止实例化的类构造函数的标准

发布于 2024-10-05 20:58:40 字数 1066 浏览 3 评论 0 原文

只是一个关于标准的问题。

我创建了一个用于 PHP 会话管理的包装类,它有助于根据访问会话数据的某些内部模块自动组织会话数据。它被设计为单例,使用 getInstance() 方法进行实例化,因为在给定时间只会有一个会话。另外,这对我来说也是一个好处,因为我能够在 session_start() 失败的情况下(尽管可能有限)防止会话对象的实例化。举个例子:

    public static function getInstance(){
        if(!self::$_instance || !session_id()){
            if(session_start()){
                self::$_instance = new self(session_id());
            }else{
                return;
            }

        }
        return self::$_instance;
    }

我的问题是;尽管出于某些原因,使用网关 getInstance() 方法在这里自然有效,但实现 public static getInstance() 或 create 是否常见/良好实践如果对象依赖于外部条件,类中的 () 方法可以控制对象的创建?

我只是发现自己坚持在单例情况下提供 getInstance() 的约定,在多个实例对象的情况下提供 create()

TL;DR:我一直使用 getInstance()create() 方法来控制所有对象实例化。我做错了吗?


编辑:稍微完善我的问题;除了使用 getInstance() 作为单例之外,我的构造函数是否用 create() 方法包装的目的较少,并且更倾向于不良约定?我应该从 true 构造函数抛出异常,还是继续从 create() 返回 false?

Just a question on standards.

I've created a wrapper class for PHP session management, that helps automatically organize session data based on certain internal modules accessing it. It's designed as a singleton, using a getInstance() method to instantiate, since there will only be a single session at a given time. Also, this came as a benefit to me, as I am able to prevent instantiation of the session object in the (albeit probably limited) chance that session_start() fails. As for an example:

    public static function getInstance(){
        if(!self::$_instance || !session_id()){
            if(session_start()){
                self::$_instance = new self(session_id());
            }else{
                return;
            }

        }
        return self::$_instance;
    }

My question is; although the use of a gateway getInstance() method works naturally here for a few reasons, is it common/good practice to implement public static getInstance() or create() methods in classes to control object creation if the object is reliant on external conditions?

I just find myself sticking to a convention of providing getInstance() in the case of singletons, and create() in the case of multiple instance objects.

TL;DR: I keep using getInstance() and create() methods to control all object instantiation. Am I doing it wrong?


EDIT: Refining my question a bit; Aside from using getInstance() for singletons, is my constructor wrapping with create() methods serving less purpose and more leaning towards bad convention? Should I be throwing Exceptions from the true constructor, or continue returning false from a create()?

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

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

发布评论

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

评论(3

述情 2024-10-12 20:58:40

单例通常被认为是“坏”的;请参阅本节了解有关该主题的激烈争论。

也就是说,使用工厂方法或工厂类来创建对象通常被认为是好的,所以你没问题:)

我个人使用 symfony 依赖注入组件(可以在不使用 symfony 框架的情况下安装在任何项目中)来简化依赖注入并在适当的情况下避免单例。

我仍然使用一些对我来说有意义的单例;例如,记录器和工厂对象对我来说似乎自然是单一的,所以我就这样制作它们。我认为,这个想法是全局功能(例如工厂)很好,但全局状态很糟糕。

关于您修改后的问题:是否抛出异常或是否从 create() 调用返回 false;这取决于您的应用程序是否可以在没有创建的对象的情况下成功继续运行。例如,如果您正在创建创建页面所需的数据库连接,则抛出异常。如果你正在做一些不太重要的事情,请返回 false 并继续你的快乐之路:)

Singletons are generally considered "bad"; see this section here for a flame war on the topic.

That said, using factory methods or factory classes to create objects is generally considered good, so you're fine there :)

I personally use the symfony dependency injection component (can be installed in any project without using the symfony framework) to simplify dependency injection and avoid singletons where it seems appropriate.

I do still use some singletons, where it makes sense to me; loggers and factory objects, for example, seem to naturally be single to me, so I make them that way. The idea is that global functionality (e.g. a factory) is fine, but global state is bad, I believe.

With regards to your amended question on whether to throw an Exception or whether to return false from your create() call; it depends on whether your application can continue successfully without the created object if not. If, for example, you were creating a database connection that is necessary to create a page, then throw an Exception. If you're doing something less essential, return false and continue on your merry way :)

獨角戲 2024-10-12 20:58:40

getInstance() 在 Zend Framework 中随处可见,这是我在代码中寻找标准和约定的首选。

至于 create(),如何使用神奇的 __construct 方法,这样当您执行 new Blah() 时,它会调用 __construct 方法那个班?

getInstance() is used ALL over the place in the Zend Framework, which is my goto for standards and conventions in code.

as for the create(), what about using the magic __construct method so that when you do new Blah() it calls the __construct method for that class?

沉鱼一梦 2024-10-12 20:58:40

您应该使用 __construct 方法,然后使用 create 方法。
由于 __construct 是自行调用的,因此您可以在 constructor 中进行初始化和其他操作。
另一个好处是您可以忘记调用 create() 方法,并且您的对象可能处于不一致的状态

You should user __construct method then using create method.
As __construct is called by itself you can do initialization and other things in constructor .
Another benefit is you can forget to call create() method and your object can be in inconsistent state

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