例一:
<?php <BR>// 类的定义 <BR>class User <BR>{ <BR>// 属性,注意public、private、protected的作用范围 <BR>public $<strong>)本文来(源gaodai#ma#com搞@@代~&码*网2</strong><pre>搞代gaodaima码
name = “hackbaby”;
// 构造函数
function __construct()
{
echo “construct
“;
}
// 方法
function say()
{
echo “这是在类的本身调用:$this->name”;
}
// 析构函数
function __destruct()
{
echo “destruct”;
}
// 返回当前对象的描述信息 通过实例化的变量名调用例如本例中的$user
function __toString()
{
return “user class”;
}
}
//实例化,如果构造函数有参数则用$user = new User(‘参数’);
$user = new User();
echo $user->name . “
“;
$user->say();
echo “
“;
echo $user;
?>
例二:
<?php <BR>class Fruit <BR>{ <BR>protected $fruit_color; <BR>protected $fruit_size; <br><br>function setcolor($color) <BR>{ <BR>$this->fruit_color = $color; <BR>} <br><br>function getcolor() <BR>{ <BR>return $this->fruit_color; <BR>} <br><br>function setsize($size) <BR>{ <BR>$this->fruit_size = $size; <BR>} <br><br>function getsize() <BR>{ <BR>return $this->fruit_size; <BR>} <br><br>function save() <BR>{ <BR>//代码 <BR>} <BR>} <BR>class apple extends Fruit <BR>{ <BR>private $variety; <br><br>function setvariety($type) <BR>{ <BR>$this->variety = $type; <BR>} <br><br>function getvariety() <BR>{ <BR>return $this->variety; <BR>} <BR>} <BR>$apple = new apple(); <BR>echo $apple->setvariety('红富士'); <BR>echo $apple->getvariety(); <BR>echo "<br />"; <BR>echo $apple->setcolor('red'); <BR>echo $apple->getcolor(); <BR>echo "<br />"; <BR>echo $apple->setsize('特大'); <BR>echo $apple->getsize(); <br><br>?><BR>