php构造函数和析构函数

来源:互联网 发布:异世淘宝女王txt下载 编辑:程序博客网 时间:2024/05/17 06:20

通俗定义
构造函数:类被实例化是调用的函数
析构函数:类被销毁时,调用的的函数
例子

<?phpclass Car {    //增加构造函数与析构函数    function __construct(){        print "***构造函数被调用*** \n";    }    public function testPrint(){        echo "测试构造函数与析构函数\n";    }    function __destruct(){        print "***析构函数被调用*** \n";    }}$car = new Car();$car->testPrint();

输出结果:

***构造函数被调用*** 测试构造函数与析构函数***析构函数被调用*** 
原创粉丝点击