脚本超时的 Final 子句替代方案
我正在训练一个 PHP 脚本作为一个表现良好的 cron 作业运行。为了避免无限循环等,我添加了 set_time_limit
。
据我所知,PHP 没有finally 子句功能。我希望有这样的功能来清理,比如在达到时间限制时取消链接文件。
实现这一目标的替代方法是什么?
I'm training a PHP script to run as a well behaved cron job. To escape infinite loops and such I've added set_time_limit
.
As far as I know there is not finally clause functionality for PHP. I would like to have such functionality to cleanup, like unlinking files when the time limit is reached.
What would be an alternative way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
布鲁斯·奥尔德里奇已经回答了如何到达。
我向您展示另一种方式:RAII 模式(并不是说更好或更坏)
示例:
如果此类将用于打开文件,则所有文件都将在脚本终止时关闭并取消链接。这个例子专门针对您的问题 - 通常使用 1 个对象来访问 1 个资源。
Bruce Aldridge already answered, how it can be reached.
I show you another way: RAII pattern (not to say that is better or worse)
Example:
If this class will be used for opening files, all files will be closed and unlinked on script termination. This example specially for your question - usually 1 object being used to give access to 1 resource.
http://php.net/manual/en/function.register-shutdown -function.php
实际上,这不会在脚本超时时运行。
并且最好在代码中处理无限循环的捕获。
http://php.net/manual/en/function.register-shutdown-function.php
actually, this won't run on script timeout.
and it would be better to handle catches for infinite loops in the code.