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;
?>

猜你喜欢

转载自blog.csdn.net/qq_40632760/article/details/82845452