dseguy, French
@dseguy@phpc.social avatar

Here is a very optional command : try-catch-finally

The finally clause in a try-catch-finally is optional: it can be omitted and often is.

The catch clauses in a try-catch-finally are also optional: they can be omitted.

When the catch and finally clauses are all omitted, the try clause can also be omitted. Safely.

https://php-tips.readthedocs.io/en/latest/tips/try-catch-finally.html

velkuns,
@velkuns@phpc.social avatar

@dseguy try/finally could be useful in some specifics cases 😁

dseguy,
@dseguy@phpc.social avatar

@velkuns I like the idea, though I'd be hard pressed to find one example.
Let me make this an @Exakat rule and see who get caught in that net first.

dseguy,
@dseguy@phpc.social avatar

@velkuns @Exakat It took 20 mins to write the rule and start auditing, but I already have the first results :

https://github.com/snc/SncRedisBundle/blob/master/src/Logger/RedisCallInterceptor.php

so, it might be more often than I though.

velkuns,
@velkuns@phpc.social avatar
dseguy,
@dseguy@phpc.social avatar

@velkuns @Exakat I'm getting more results, and it seems quite common, in fact.

May be even more than a __destruct() function.

May be I'll take some time to review them.

velkuns,
@velkuns@phpc.social avatar

@dseguy @Exakat one use case in mind is when I open a resource (file for example).

I need to close it in any case. But not necessary to catch any exception. So just try/finally can help to avoid duplicate code to close the resource.

dseguy,
@dseguy@phpc.social avatar

@velkuns @Exakat I am thinking about external resources that needs to be finished absolutely, like XML file (closing all tags before the file).

I'm also thinking about releasing stuff, like a lock or a connexion before moving on.

velkuns,
@velkuns@phpc.social avatar

@dseguy @Exakat yes.

Lots of use cases, finally 😁

Thanks to PHP to permit this kind of thing ❤️

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • Leos
  • DreamBathrooms
  • InstantRegret
  • mdbf
  • khanakhh
  • magazineikmin
  • Durango
  • Youngstown
  • slotface
  • rosin
  • everett
  • kavyap
  • cisconetworking
  • ngwrru68w68
  • megavids
  • tacticalgear
  • osvaldo12
  • GTA5RPClips
  • ethstaker
  • thenastyranch
  • cubers
  • anitta
  • tester
  • modclub
  • normalnudes
  • provamag3
  • JUstTest
  • lostlight
  • All magazines