Is destructor in PHP predictable?

前端 未结 2 1789
旧巷少年郎
旧巷少年郎 2021-01-18 06:37

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

2条回答
  •  再見小時候
    2021-01-18 07:00

    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.

提交回复
热议问题