I'm coming from C++ where I regularly employ RAII.
PHP, as far as I know, uses automatic memory management, so is it still applicable/good practice to use RAII through constructors/destructors with PHP?
I'm coming from C++ where I regularly employ RAII.
PHP, as far as I know, uses automatic memory management, so is it still applicable/good practice to use RAII through constructors/destructors with PHP?
To my knowledge, I don't believe RAII can be implemented in PHP. Destructors are not guaranteed to be called when the associated object scopes but rather whenever the PHP runtime deems the object is no longer referenced. As a result, it may not be as reliable for RAII. It's probably worth reading through this documentation in detail:
As RAII is used for lock threads, files operation. In php you doesn't have an threads, just forks. Nevermind, allocated and created the core object into constructor and free memory in destructor it's still good practice even since php 5.3 have very good garbage collector.