mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
Mutable objects can be challenging to work with in larger codebases because different parts of a system may modify the same instance, making it difficult to trace where and when changes occurred. This becomes especially problematic when debugging unexpected behaviour. By making `Validator` immutable, we ensure that adding rules via `with()` returns a new instance rather than mutating the original, and we use the `with()` method inside `__call()`, making every call to a rule into a clone of the current `Validator`. This provides several benefits: 1. Predictability: A `Validator` instance will always behave the same way throughout its lifetime, regardless of what other parts of the codebase do. 2. Safe dependency injection: Users can now confidently inject a base `Validator` from a DI container, knowing that any modifications made elsewhere will not affect their instance. 3. Easier debugging: Since validators cannot be mutated after creation, there's no need to track down where an unexpected rule was added. 4. Reusability: Users can create an initial `Validator` with some base rules and reuse it by just adding new rules to the chain without affecting the base `Validator`. |
||
|---|---|---|
| .. | ||
| rules | ||
| 01-installation.md | ||
| 02-feature-guide.md | ||
| 03-handling-exceptions.md | ||
| 04-message-translation.md | ||
| 05-message-placeholder-conversion.md | ||
| 06-concrete-api.md | ||
| 07-custom-rules.md | ||
| 08-comparable-values.md | ||
| 09-list-of-rules-by-category.md | ||
| 10-license.md | ||
| comparing-empty-values.md | ||
| index.md | ||