简体   繁体   中英

PHP: How to clear numerous static variables for a queued Laravel job?

I have a typical Laravel application, making full use of Eloquent. Part of the application deals with payroll.

Doing a 'payroll run' requires a lot of small tasks to run one after the other. Some of these tasks are required by other parts of the application, eg PayrollRunner::getNextPayrollDates() , so I made them static. Static methods led me toward using static class properties, and soon I found myself writing the whole 'payroll runner' using static methods and static properties.

It's the first time I have really made heavy use of statics, and I have to say that it felt really natural for this use case.

Once I had the payroll working well, I started triggering it from a queued job. (It can take quite a while to process). But since the artisan queue reads the entire application into memory when it boots, the statics remain set until the queue is killed.

Is there any easy way to deal with this? My PayrollRunner class has a large number of static methods and properties, so rewriting it object-style is a last resort. Is there a way to reset all static properties on a class without cycling through each one?

I see this answer says no, Is there any way to reset all static properties of a particular class? , but it is quite old.

Alternatively, is there a way to start a Laravel queue that doesn't preserve the state of statics between jobs?

It would be way better to avoid this in the first place, but you could keep running a queue worker that runs a single queue job. It wouldn't be in "daemon" mode. After a job is processed it will end and a new worker will need to be started.

php artisan queue:work --once

New PHP process, new state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM