Hi Olle Härstedt,

> I'd like to discuss the concept of ownership in PHP, from the point of
> view of a new interface UniqueInterface (or SplUniqueInterface or
> something), which will throw a UniqueException if refcount > 1.

It'd seem like it would cause a lot of places where UniqueException could be 
thrown unexpectedly.
Additionally, inspecting the stack trace of an exception would also increment 
the refcount,
causing yet another exception.

```
<?php
class UniqueObject{}
function test(UniqueObject $x) {
    // ...
    // handle some edge case

    // $trace includes an array with a reference to $x at $trace[0]['args'][0],
    // increasing the refcount to 2, throwing.
    //
    // Error handlers and issue handlers would also have this issues,
    // as well as Throwable->getTrace()
    $trace = debug_backtrace();
    var_dump($trace);
}
test(new UniqueObject());
```

> Implementation should be straight-forward, assuming refcount is manipulated 
> at a
> single place in the source.

This would also introduce some performance overhead if every function that 
incremented the reference count of a value
now had to check if objects implemented that interface. I'd assume it would be 
significant.

This is something that makes more sense in compiled languages like rust that 
can enforce uniqueness at compile time,
and the dynamic nature of php makes that less practical.

- Tyson

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

Reply via email to