1441 |
ariadna |
1 |
# A trait that allows you to clone readonly properties in PHP 8.1
|
|
|
2 |
|
|
|
3 |
[](https://packagist.org/packages/spatie/php-cloneable)
|
|
|
4 |
[](https://github.com/spatie/php-cloneable/actions?query=workflow%3ATests+branch%3Amaster)
|
|
|
5 |
[](https://github.com/spatie/php-cloneable/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amaster)
|
|
|
6 |
[](https://packagist.org/packages/spatie/php-cloneable)
|
|
|
7 |
|
|
|
8 |
This package provides a trait that allows you to clone objects with readonly properties in PHP 8.1. You can read an in-depth explanation as to why this is necessary [here](https://stitcher.io/blog/cloning-readonly-properties-in-php-81).
|
|
|
9 |
|
|
|
10 |
## Support us
|
|
|
11 |
|
|
|
12 |
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/php-cloneable.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/php-cloneable)
|
|
|
13 |
|
|
|
14 |
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
|
|
|
15 |
|
|
|
16 |
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
|
|
|
17 |
|
|
|
18 |
## Installation
|
|
|
19 |
|
|
|
20 |
You can install the package via composer:
|
|
|
21 |
|
|
|
22 |
```bash
|
|
|
23 |
composer require spatie/php-cloneable
|
|
|
24 |
```
|
|
|
25 |
|
|
|
26 |
## Usage
|
|
|
27 |
|
|
|
28 |
In PHP 8.1, readonly properties aren't allowed to be overridden as soon as they are initialized. That also means that cloning an object and changing one of its readonly properties isn't allowed. It's likely that PHP will get some kind of `clone with` functionality in the future, but for now you can work around this issue by using this package.
|
|
|
29 |
|
|
|
30 |
```php
|
|
|
31 |
class Post
|
|
|
32 |
{
|
|
|
33 |
use Cloneable;
|
|
|
34 |
|
|
|
35 |
public readonly string $title;
|
|
|
36 |
|
|
|
37 |
public readonly string $author;
|
|
|
38 |
|
|
|
39 |
public function __construct(string $title, string $author)
|
|
|
40 |
{
|
|
|
41 |
$this->title = $title;
|
|
|
42 |
$this->author = $author;
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
```
|
|
|
46 |
|
|
|
47 |
The `Spatie\Cloneable\Cloneable` adds a `with` method to whatever class you want to be cloneable, which you can pass one or more parameters. Note that you're required to use named arguments.
|
|
|
48 |
|
|
|
49 |
```php
|
|
|
50 |
$postA = new Post(title: 'a', author: 'Brent');
|
|
|
51 |
|
|
|
52 |
$postB = $postA->with(title: 'b');
|
|
|
53 |
$postC = $postA->with(title: 'c', author: 'Freek');
|
|
|
54 |
```
|
|
|
55 |
|
|
|
56 |
A common practice would be to implement specific `with*` methods on the classes themselves:
|
|
|
57 |
|
|
|
58 |
```php
|
|
|
59 |
class Post
|
|
|
60 |
{
|
|
|
61 |
// …
|
|
|
62 |
|
|
|
63 |
public function withTitle(string $title): self
|
|
|
64 |
{
|
|
|
65 |
return $this->with(title: $title);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public function withAuthor(string $author): self
|
|
|
69 |
{
|
|
|
70 |
return $this->with(author: $author);
|
|
|
71 |
}
|
|
|
72 |
}
|
|
|
73 |
```
|
|
|
74 |
|
|
|
75 |
### Caveats
|
|
|
76 |
|
|
|
77 |
- This package will skip calling the constructor when cloning an object, meaning any logic in the constructor won't be executed.
|
|
|
78 |
- The `with` method will do a shallow clone, meaning that nested objects aren't cloned as well.
|
|
|
79 |
|
|
|
80 |
## Testing
|
|
|
81 |
|
|
|
82 |
```bash
|
|
|
83 |
composer test
|
|
|
84 |
```
|
|
|
85 |
|
|
|
86 |
## Changelog
|
|
|
87 |
|
|
|
88 |
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
|
|
|
89 |
|
|
|
90 |
## Contributing
|
|
|
91 |
|
|
|
92 |
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
|
|
|
93 |
|
|
|
94 |
## Security Vulnerabilities
|
|
|
95 |
|
|
|
96 |
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
|
|
|
97 |
|
|
|
98 |
## Credits
|
|
|
99 |
|
|
|
100 |
- [Brent Roose](https://github.com/brendt_gd)
|
|
|
101 |
- [All Contributors](../../contributors)
|
|
|
102 |
|
|
|
103 |
## License
|
|
|
104 |
|
|
|
105 |
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|