php-ood构造和析构-134

<?php

class MyTest{

public$name;

public$type;

function writer(){

return$this->name."巴金";

   }

function prdate(){

return$this->writer()."2012.7";

   }

}

$book1=new MyTest();

$book2=new MyTest();

$book1->name="";

$book2->name="春秋";

echo$book1->writer()."</br>";

echo$book2->prdate();

?>

http://localhost/php/18.php

家巴金
春秋巴金2012.7

====================================================

<?php

/*

* Created on 2012-7-11

*

* To change the template for this generatedfile go to

* Window - Preferences - PHPeclipse - PHP -Code Templates

*/

class MyTest{

public$name;

public$type;

//php5定义构造函数

function __construct($name='',$type=''){

$this->name=$name;

$this->type=$type;

   }

//定义方法

function writer(){

return$this->name.$this->type."巴金";

   }

function prdate(){

return$this->writer()."2012.7";

   }

//定义析构

function __destruct(){

echo"¡á¡á¡á调用析构:".$this->name."</br>";

   }

}

$book1=new MyTest('','sanwen');

$book2=new MyTest('春秋','biji');

//$book1->name="";

//$book2->name="春秋";

echo$book1->writer()."</br>";

$book1=null;

echo$book2->prdate()."</br>";

$book2=null;

?>

http://localhost/php/18.php

sanwen巴金
×××调用析构:家
春秋biji巴金2012.7
×××调用析构:春秋


你可能感兴趣的:(PHP)