Is destructor in PHP predictable?

北城余情 提交于 2019-12-01 15:33:20

问题


Is a class destructor in PHP predictable? When is the destructor called?

Like in many languages, will a class destructor be called as soon as the object goes out of scope?


回答1:


PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.

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




回答2:


It's called when the first of these conditions are met:

  • The reference count of the object goes to 0 (these usually happens when the object has no more variables that reference it -- they were unset or went out of scope --, but it can happen later, as an object may be referenced by something other than a variable -- in fact, the reference count is just a number and can be manipulated in an arbitrary way).
  • When using PHP 5.3, when the garbage collector detects the positive reference count is due to circular references.
  • Otherwise, when the script finishes cleanly.

In short, you should not rely on it always being called, because the script may not finish cleanly.



来源:https://stackoverflow.com/questions/3325979/is-destructor-in-php-predictable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!