码迷,mamicode.com
首页 > Web开发 > 详细

php 析构函数,构造函数

时间:2017-07-23 19:55:05      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:str   new t   put   类的属性   dump   ret   turn   return   生日   

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

php 析构函数,构造函数

标签:str   new t   put   类的属性   dump   ret   turn   return   生日   

原文地址:http://www.cnblogs.com/cp168168/p/7225395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!