為什么加了echo“hello"后,hello會夾在二個詞中間
<?php
class Car {
? ? function __construct(){
? ? ? ? print "構造";
? ? }
? ? function __destruct(){
? ? ? ? print "析構";
? ? }
? ? //增加構造函數與析構函數
}
$car = new Car();
echo "hello";
這樣的結果是 ? ?“構造hello析構” ?為什么不是"構造析構hello",有構造函數的類創建實例化對象后會直接調用,調用后會刪除這個對象,這是析造函數會自動調用,那不是應該“構造跟析造”這二個詞緊貼在一起的嗎,為什么會“析造”會在hello的后面,請大神詳解?。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。?!
2016-09-12
$car = new Car();
unset($car);// 手動解構
echo "hello";
應該是程序沒有完結時這個對象還一直被占用吧
2016-09-12
<?php
class user{
? ? private static $count = 0 ; //記錄所有用戶的登錄情況.
? ? public function __construct(){
? ? ? ? self::$count = self::$count + 1;
? ? }
? ? public function getCount(){ ? ?
? ? ? return self::$count;
? ? }
? ? public function __destruct(){
? ? ? ? self::$count = self::$count -1;
? ? }
}
$user1 = new user();
$user2 = new user();
$user3 = new user();
echo "now here have ".$user3->getCount()." user";
echo "<br>";
unset( $user3);
echo "now here have ".$user2->getCount()." user";
?>
結果是:
now here have 3 user
now here have 2 user
實例化三次所以調用構造函數三次結果是3,然后在unset顯式銷毀的時候會自動調用析構函數因此結果是2
2016-09-12
只有在頁面代碼全部執行完畢后php才會釋放變量,然后調用析構函數,所以只有在echo"hello"執行完畢后才會調用,所以結果是"構造hello析構"