the way you explain concepts is truly amazing, you truly understand everything about php so deep and you really are a great tutor than any other nextjs tutor out there, please start teaching us Nextjs because, unfortunately, i still hate php to the core!!
I gotta give it to you, that's a smart way of advertising and not be annoying for the viewer. Always looking out for more videos of yours. Also php is kinda cool imo, we've got a lot of useful syntax other languages don't even bother with, like falsy-safe operator, traits, arrow functions (I actually love the `fn` part), we'll probably even get structs so OOP haters can deal away with classes completely (thank god!). We just need those native generics to land sometime and it will be a breeze!
I'll go with the stack concept as it gives us a better control on when to cleanup. BTW, PHP isn't bad, it's just the way we write our code, although I agree with everything else. Thanks Aaron!
unset($foo) the GC will remove $foo when its no longer needed, and $foo = null immediately releases the memory, maybe also look at PHP weakmaps and lazy objects
I've been dabbling in some C recently, because I've had this idea gnawing at me for months: "what would it look like if PHP had a `defer` keyword" This is exactly the type of stuff that could use it.
@@aarondfrancis I learned about the `defer` keyword when working through `Building an Interpreter in Go`, and it basically just keeps cleanup code right next to the other stuff, and runs it last. So you can use it to close file handles, close socket connections, etc. So you'd do something like ``` $this->startNgrok(); defer $this->stopNgrok(): // ... ``` It'd take some real thought work, like, does the a deferred action still run after an exception is thrown, etc. I think exploring other languages has been super useful!
@@LenWoodward I understand Defer in an Async scenario, not quite sure what the intent is in your example? If its about freeing up memory then one can use PHP unset(...) or null assignment, with unset($foo) the GC will remove $foo when its no longer needed, and $foo = null; immediately releases the memory. But this is probably not what you meant?
@@depafrom5277 that's not quite what I meant, because you're right, freeing up memory by unsetting variables is already handled by the garbage collector. This would be a way to make sure that we can group cleanup code next to the code that makes it necessary. In this context cleanup doesn't refer to memory, it could really apply to anything. Like if we're saving a .lock file somewhere, doing some work, then deleting the .lock file. Now we could just have `defer File::delete('action.lock');` on the line after we create it. This is a poor example, but still, we could use it to close open file handles, sockets that have been left open, to kill background processes that we spawned, etc
Nice tips and tricks (and I'm thirsty) In php there is "register_shutdown_function" which can be useful because it execute even after a timeout In go there is something similar using the defer keyword
Great video as usual and an interesting topic! I like the way you declare the start and cleanup functions together, but I think the first option is a bit messy and pollutes the main method with variables and unnecessary reason to change(adding callback logic). The second method is much cleaner. The cleanup method could probably also be in a __destruct().
Just watched the new Deadpool recently and I can't help but confuse you with Ryan Reynolds because of your profile picture haha. Anyway, keep up the good work, Aaron!
Problem is __destruct is called just before the end of code execution, and one will probably want to "clean up" during code execution - unset($foo) the GC will remove $foo when its no longer needed, and $foo = null immediately releases the memory, maybe also look at PHP weakmaps and lazy objects.
True! Probably best to wrap the individual failable actions in a try catch, or every call in a try catch. These two should never fail because I check that the process is running and I used the @ sign to suppress the unlink errors, but still a good practice.
Prefer to read instead of watching? Check out the article version of this video: aaronfrancis.com/2024/clean-up-after-yourself-618236cb
Genuinely a very refreshing format!
I got really excited about the article version that I can always return to! Love it!
Thnaks for "Sleep: : for(1)->seconds ;"! I can't imagine a better example to demontarate overuse of OOP :)
There's that intro! As someone who just recently started learning Laravel, I like this series idea.
This was great! Love these patterns.
the way you explain concepts is truly amazing, you truly understand everything about php so deep and you really are a great tutor than any other nextjs tutor out there, please start teaching us Nextjs because, unfortunately, i still hate php to the core!!
What a genuinely great tutorial Learnt something new today.
This is amazing, keep it going Mr Francis
I gotta give it to you, that's a smart way of advertising and not be annoying for the viewer.
Always looking out for more videos of yours.
Also php is kinda cool imo, we've got a lot of useful syntax other languages don't even bother with, like falsy-safe operator, traits, arrow functions (I actually love the `fn` part), we'll probably even get structs so OOP haters can deal away with classes completely (thank god!).
We just need those native generics to land sometime and it will be a breeze!
I'll go with the stack concept as it gives us a better control on when to cleanup.
BTW, PHP isn't bad, it's just the way we write our code, although I agree with everything else.
Thanks Aaron!
unset($foo) the GC will remove $foo when its no longer needed, and $foo = null immediately releases the memory, maybe also look at PHP weakmaps and lazy objects
If you haven’t tried the pineapple spindrift, definitely add that one to the list. My favorite flavor, as a spindrift connoisseur
Looking forward for next topics keep going cool vibes🍹
I've been dabbling in some C recently, because I've had this idea gnawing at me for months: "what would it look like if PHP had a `defer` keyword"
This is exactly the type of stuff that could use it.
Oh that's interesting... I don't know what that means but it sounds excellent
@@aarondfrancis I learned about the `defer` keyword when working through `Building an Interpreter in Go`, and it basically just keeps cleanup code right next to the other stuff, and runs it last. So you can use it to close file handles, close socket connections, etc.
So you'd do something like
```
$this->startNgrok();
defer $this->stopNgrok():
// ...
```
It'd take some real thought work, like, does the a deferred action still run after an exception is thrown, etc. I think exploring other languages has been super useful!
@@LenWoodward I understand Defer in an Async scenario, not quite sure what the intent is in your example?
If its about freeing up memory then one can use PHP unset(...) or null assignment, with unset($foo) the GC will remove $foo when its no longer needed, and $foo = null; immediately releases the memory. But this is probably not what you meant?
@@depafrom5277 that's not quite what I meant, because you're right, freeing up memory by unsetting variables is already handled by the garbage collector.
This would be a way to make sure that we can group cleanup code next to the code that makes it necessary. In this context cleanup doesn't refer to memory, it could really apply to anything. Like if we're saving a .lock file somewhere, doing some work, then deleting the .lock file. Now we could just have `defer File::delete('action.lock');` on the line after we create it. This is a poor example, but still, we could use it to close open file handles, sockets that have been left open, to kill background processes that we spawned, etc
Love this!
That intro was superb 👌
Nice tips and tricks (and I'm thirsty)
In php there is "register_shutdown_function" which can be useful because it execute even after a timeout
In go there is something similar using the defer keyword
The first approach was functional and, I’d argue, the best. If only php supported defer keyword or destructors, it would have been even better.
Love you Aaron 😍
Great video as usual and an interesting topic!
I like the way you declare the start and cleanup functions together, but I think the first option is a bit messy and pollutes the main method with variables and unnecessary reason to change(adding callback logic). The second method is much cleaner. The cleanup method could probably also be in a __destruct().
Just watched the new Deadpool recently and I can't help but confuse you with Ryan Reynolds because of your profile picture haha. Anyway, keep up the good work, Aaron!
Nice video !
What color scheme are you using in your IDE please ?
Spindrift lemon rules
Number one (so far)
Spindrift is goated! I drink way too many lol
Any downside to processing the callback stack in __destruct()?
Oh that's an interesting idea! Not that I can think of at the momemt
Problem is __destruct is called just before the end of code execution, and one will probably want to "clean up" during code execution - unset($foo) the GC will remove $foo when its no longer needed, and $foo = null immediately releases the memory, maybe also look at PHP weakmaps and lazy objects.
Awesome
Thanks.
Awesome video as well. Do you dislike dark themes? 😂
This channel is approaching MKBHD levels of polish. 👌
Will you continue with LifeOS live series?
Yes! Just wrapping up a few things this week
Okay good tip, but using *lemon* as your Spindrift pick will really hurt the rankings. Demand a redraw with either raspberry lime or pineapple.
Don't worry, we're doing all flavors!
Spindrift #1
What about exceptions? If one of your cleanup function throws then the remaining functions will not be called
True! Probably best to wrap the individual failable actions in a try catch, or every call in a try catch. These two should never fail because I check that the process is running and I used the @ sign to suppress the unlink errors, but still a good practice.
Is it legal to do functional programming in PHP?
Are you open to sponsorships from beverage companies yet?
1000% my beverage integrity is for sale
Love it! Reminds me of the "defer" keyword in Go
first