1

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?

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
  • possible duplicate of [Does PHP support the RAII pattern? How?](http://stackoverflow.com/questions/4938679/does-php-support-the-raii-pattern-how) – Prof. Falken Aug 09 '13 at 11:38

2 Answers2

0

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:

http://php.net/manual/en/language.oop5.decon.php

ScoPi
  • 1,193
  • 9
  • 14
0

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.

cojack
  • 2,444
  • 1
  • 17
  • 18