Is the __destruct method necessary for PHP?

前端 未结 3 734
一个人的身影
一个人的身影 2020-12-16 21:04

The manual said that

The destructor method will be called as soon as all references to a particular object are removed or when the object is explic

3条回答
  •  悲&欢浪女
    2020-12-16 21:18

    If you're using exceptions, you should try to follow RAII even with PHP. And you must use destructors for RAII. It was invented for C++ but the same logic works with PHP, too. Be warned that in some cases (e.g. fatal error or when exit() is called) PHP may end up calling the destructors in incorrect order which I consider a bug in PHP engine. For normal code and exceptions, the PHP engine seems to work as if it was C++ program.

    See also: Can I trust PHP __destruct() method to be called?

提交回复
热议问题