PHP在类对象数组中取消类对象的设置会调用所有类对象的析构函数吗?
我有一个存储在PHP数组中的客户端类对象数组。有时我需要PHP在类对象数组中取消类对象的设置会调用所有类对象的析构函数吗?,php,Php,我有一个存储在PHP数组中的客户端类对象数组。有时我需要unset()数组中的一个对象,我不想担心显式关闭套接字连接。我想让\uu destruct()帮我做这件事 class A{ private $id; public function __construct( $id ){ $this->id = $id; } public function __destruct(){ echo "Calling destruct
unset()
数组中的一个对象,我不想担心显式关闭套接字连接。我想让\uu destruct()
帮我做这件事
class A{
private $id;
public function __construct( $id ){
$this->id = $id;
}
public function __destruct(){
echo "Calling destruct on " . $this->id . ".\n";
}
}
$array = array();
for($i=0;$i<5;$i++){
$array[$i] = new A($i);
}
unset($array[3]);
print_r($array);
为什么会发生这种情况?我有什么选择?如果
$array
超出范围,那么其余的对象也将被销毁
看起来这种情况正在发生。您将看到析构函数跟踪消息,然后是print_r()输出,然后是其余对象的析构函数。由于脚本结束,正在调用析构函数。。明白了,这也行。:)我不确定这是否是脚本的结尾。是的,但这是一个守护进程。。这永远不会结束:)到目前为止,还没有结束。我认为不会。为什么?我误解了你关于守护进程永无止境的评论,认为你的代码没有退出。在这种情况下,我想说,如果您的守护进程正在分叉进程以并行处理其任务,那么退出的子进程将在输出中产生相同的效果。但你不会这么做的,所以别担心
Calling destruct on 3.
Array
(
[0] => A Object
(
[id:A:private] => 0
)
[1] => A Object
(
[id:A:private] => 1
)
[2] => A Object
(
[id:A:private] => 2
)
[4] => A Object
(
[id:A:private] => 4
)
)
Calling destruct on 0.
Calling destruct on 1.
Calling destruct on 2.
Calling destruct on 4.