為什么實例化對象析構函數會被調用
<?php
class Car {
? ? //增加構造函數與析構函數
? ? function __construct(){
? ? ? ? print '父類構造函數被調用 \n';
? ? }
? ? function __destruct(){
? ? ? ? print '析構函數被調用 \n';
? ? ? ??
? ? }
}
$car = new Car(); ?//實例化時會調用構造函數
<?php
class Car {
? ? //增加構造函數與析構函數
? ? function __construct(){
? ? ? ? print '父類構造函數被調用 \n';
? ? }
? ? function __destruct(){
? ? ? ? print '析構函數被調用 \n';
? ? ? ??
? ? }
}
$car = new Car(); ?//實例化時會調用構造函數
2017-05-25
舉報
2017-06-14
當PHP代碼執行完畢以后,會自動回收與銷毀對象
這樣你應該能明白
2017-05-25
如果你在$car = new Car();后面打上echo “hello”;
你會發現瀏覽器打印的是 ?構造 ?hello ?析構?
為什么不是 構造?析構?hello?
原因是php頁面執行完請求后,內存中就銷毀它了,同時這個頁面創造的對象就會被銷毀
對象被銷毀,不就自動調用析構函數了?