To differentiate between singletons that persist and those that don't in Laravel Octane, you need to understand how Octane handles singleton instances and how they are resolved. Here are the key points to consider:
Singletons That Persist
1. Singletons in Service Providers: Singletons that are resolved inside service providers, specifically in the `boot` or `register` methods, will persist across requests. This is because these singletons are resolved during the application bootstrapping and are stored in the container cache.
2. Warm Services: You can add singletons to the `warm` array in the Octane configuration file to ensure they are resolved and persisted during the application bootstrapping. This way, these singletons will be available across all requests.
Singletons That Don't Persist
1. Singletons in Request Handling: Singletons that are resolved during request handling will not persist. They are constructed on every request and are not stored in the container cache.
2. Flush Services: If you have a package that registers and resolves a singleton inside a service provider and you want to flush that instance before every request, you can add it to the `flush` array in the Octane configuration file. This ensures that the singleton is removed from the container after handling each request.
Best Practices
1. Use Callbacks: When registering singletons, use callbacks instead of the actual instance to ensure they are resolved only once.
2. Avoid Static Properties: Avoid using static properties in your singletons as they can lead to memory leaks and persist across requests.
3. Use Constants: Use constants instead of static properties when possible to ensure immutability.
4. Request Instance: When resolving singletons, pass a callback to ensure the request instance is resolved correctly.
5. Configuration Repository: Use the `config()` helper function instead of injecting the configuration repository to ensure it is resolved correctly.
By following these best practices and understanding how Octane handles singleton instances, you can effectively manage and differentiate between singletons that persist and those that don't in your Laravel application running on Octane[2][4][5].
Citations:[1] https://laracasts.com/discuss/channels/laravel/singletone-not-working-in-laravel
[2] https://divinglaravel.com/laravel-octane-bootstrapping-the-application-and-handling-requests
[3] https://grantwinney.com/difference-between-singleton-scoped-transient/
[4] https://betterprogramming.pub/the-downsides-of-coding-with-laravel-octane-17f4a7a4ea85?gi=d8afa3954408
[5] https://www.prismetric.com/laravel-octane/