1. 程式人生 > >php之(__construct)和(__destruct)

php之(__construct)和(__destruct)

<?php
    class Person{
        private $teacher;
        private $student;
        /*如果子類中定義了建構函式則不會隱式呼叫其父類的建構函式。要執行父類的建構函式,需要在子類的建構函式中呼叫 parent::__construct()。如果子類沒有定義建構函式則會如同一個普通的類方法一樣從父類繼承(假如沒有被定義為 private 的話)。*/
        function __construct($teacher,$student){
            $this->teacher=$teacher;
            $this->student=$student;
        }
        function toString(){
            echo ($this->teacher);
        
        }
        function __destruct(){//當銷燬的時候呼叫這個方法
            echo "<br/>boom";
        }
    }
    $person=new Person("cyc","ccy");
    echo $person->toString();
    $person=Null;
?>