Re: Possible RFC: UniqueInterface that throws exception at refcount > 1

From: Date: Sat, 11 Jul 2020 22:17:33 +0000
Subject: Re: Possible RFC: UniqueInterface that throws exception at refcount > 1
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-110959@lists.php.net to get a copy of this message
On Sun, Jul 12, 2020 at 12:47 AM Olle Härstedt <olleharstedt@gmail.com> wrote: > You're misunderstanding. :) Yes, it should be shared, but not with the > same access level as the owner. If a dependent class closes the > connection it will cause spooky-action-at-a-distance somewhere else in > the program. Sorry, it's related to refcount as ownership checking and > not to throwing exception at refcount > 1. If your class doesn't know whether it created its connection or it was passed to it, it's badly designed. Not only would that mix concerns (making DB requests and managing connections), it would also be hard to test and hard to keep in the programmer's head. Use dependency injection to avoid that. I'm not convinced by this example. You can't use ownership as a protection against code doing something stupid. Imagine for a second you've prevented non-owners from closing your connection. Will that also prevent non-owners from dropping your database? What about them messing with your connection object's properties? Language features can only prevent developers from making accidental mistakes. Bad code is not an accidental mistake. -- Best regards, Max Semenik

« previous php.internals (#110959) next »