1 <?php 2 header("content-type:text/html; charset=utf-8"); 3 error_reporting(E_ALL); 4 5 class MyClass { 6 7 var $id;//声明变量 8 9 function __construct($name){ 10 echo "构造函数:类被实例化后自动调用<br/>"; 11 $this->name = $name;//声明并初始化变量 12 } 13 14 function printer($con){ 15 echo isset($con) ? $con : "空"; 16 echo "<br/>"; 17 } 18 19 function sayName(){ 20 echo $this->name; 21 echo "<br/>"; 22 } 23 24 function getID(){ 25 return $this->id; 26 } 27 28 function __destruct(){ 29 echo "析构函数:对象结束生命周期时自动调用"; 30 } 31 32 } 33 34 $obj = new MyClass("weihexin"); 35 36 $obj->sayName(); 37 38 $obj->printer($obj->getID());