On Sat, Jul 11, 2020, at 4:04 PM, Olle Härstedt wrote:

> > I think that would do a far better job of addressing the
> > shared-mutable-state issue than reference counting, because it attacks the
> > root problem rather than a symptom.
> >
> > --Larry Garfield
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: https://www.php.net/unsub.php
> 
> I think freezing objects might be better fit for my imagined use-case.
> The only problem I see is that you can't really unfreeze them. Imagine
> a database connection that only can be open/closed at refcount = 1:
> 
> ```
> $connection = new OwnershipConnection();
> $connection->open();
> $ps = new PostService($connection);
> $ps->updateAllPosts();  // Throws exception if $connection->close()
> $connection->close();
> ```
> 
> With freeze, you could also do
> 
> ```
> $ps = new PostService($connection->freeze());
> ```
> 
> to ensure it's not closed by mistake. But then you couldn't close the
> connection at all, except in __destruct.
> 
> Especially, CoW for objects (at opt-in) is not a replacement where
> ownership is supposed to replace immutability for performance reason,
> e.g. creating a separate immutable database connection for every class
> that uses it.
> 
> Immutable builder are already part of PSR, e.g. here:
> https://www.php-fig.org/psr/psr-7/
> 
> I have to wonder how reasonable this is, when freezing or ownership
> are also relevant solutions, with different trade-offs.
> 
> Olle

A database connection is the textbook example of an object that *should* be 
shared; giving a separate connection to every service object is only going to 
waste time opening multiple connections and waste open connections.  Please 
don't do that.

Your example seems directly contrary to the goal you're describing.

--Larry Garfield

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to