php 析构函数,构造函数

php 析构函数,构造函数

 
<?php

/**
* 测试使用的PHP操作类
* Date: 2017/7/13
* Time: 14:22
*/
class Test
{
/** 姓名 */
public $name;
/** 生日 */
public $birth;

/**
* 构造函数方法 __construct 它是一个魔术方法
* 它是在创建对象时被自动调用
* 一个类中有且只能有一个构造函数
* 构造函数可以带参数,这些参数通常是用来给类的属性进行初始化赋值
*/
public function __construct($name='', $birth='')
{
// $this 它指代的是当前类的对象
$this->name = $name;
$this->birth = $birth;
echo '<br><br>调用了构造函数<br><br>';
}

/**
* 析构函数 __destruct 也是一个魔术方法
* 它是在对象被销毁时自动被调用
* 它不能带参数
*/
public function __destruct()
{
echo '<br><br>调用了析构函数<br><br>';
}

/**
* 测试使用的方法
* @param 参数值说明
* @return 返回值的说明
*/
public function testOutput()
{
echo '测试输出';
}
}

$test = new Test('王五', '1996-06-12');
var_dump($test);
echo '<br><br>';
$test->name = '李四';

var_dump($test);
echo '<br><br>';
$test->testOutput();
$test = null;
//$test->__construct();

echo '<br><br>执行完毕<br><br>';
原文地址:https://www.cnblogs.com/cp168168/p/7225395.html