Sorry, I missed the first question.

>  "Random class can be serialized or cloned if the algorithm supports it."
It isn't clear to me how that support is defined in a userland
implementation? Simply by implementing __serialize/__unserialize?

Userland implementations can be serialize / unserialize using the standard
PHP serialization mechanism.

For example, the following:

```php
class UserRNG implements RandomNumberGenerator
{
    protected int $current = 0;

    public function generate(): int
    {
        return ++$this->current;
    }
}

$random = new Random(new UserRNG());
$random->nextInt();
$random_serialized = serialize($random);
var_dump($random_serialized);
//
O:6:"Random":2:{i:0;a:1:{s:11:"Randomrng";O:7:"UserRNG":1:{s:10:"*current";i:1;}}i:1;N;}

$random_unserialized = unserialize($random_serialized);
var_dump($random->nextInt() === $random_unserialized->nextInt()); // true
```

Regards,
Go Kudo

2021年6月2日(水) 17:51 Jordi Boggiano <j.boggi...@seld.be>:

> On 01/06/2021 16:28, Go Kudo wrote:
> > Hello internals.
> > Thanks for continuing to participate in the discussion.
> >
> > I've sorted out the proposal, and finished writing and implementing the
> RFC.
> > (Funny, I know.) I think it's time to start a discussion.
> >
> > https://wiki.php.net/rfc/rng_extension
> > https://github.com/php/php-src/pull/7079
> >
> > The main changes since last time are as follows:
> >
> > - The ugly RANDOM_USER has been removed.
> > - RandomNumberGenerator interface has been added for user-defined RNGs.
> > - Random class is now final.
> > - Random class now accepts a RandomNumberGenerator interface other than
> > string as the first argument to the constructor.
> > - INI directive has been removed. In 32-bit environments, the result is
> > always truncated.
>
> Overall this looks much better! From a PHP userland perspective I can't
> see any huge problem at first glance.
>
> Just a few notes:
>
> - "Random class can be serialized or cloned if the algorithm supports
> it." It isn't clear to me how that support is defined in a userland
> implementation? Simply by implementing __serialize/__unserialize?
> - The __unserialize docblock has two typos (Useri*i*alize and *in*
> instead of if)
> - If an object is passed to `new Random($obj)`, probably it should throw
> an InvalidArgumentException if a seed is also passed in, as I assume in
> that case it would be otherwise be ignored.
>
> Best,
> Jordi
>
> --
> Jordi Boggiano
> @seldaek - https://seld.be
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>

Reply via email to