php>class Name extends Another Class { Aclearcase/" target="_blank" >ccess Variable Declaration Access Function Declaration }
<?php
//定义一个跟踪用户的类
class User
{
//属性
public $name;
private $password, $lastLogin;
//方法
public function __construct($name, $password)
{
$this->name = $name;
$this->password = $password;
$this->lastLogin = time();
$this->accesses++;
}
// 获取最后访问的时间
function getLastLogin()
{
return(date("M d Y", $this->lastLogin));
}
}
//创建一个对象的实例
$user = new User("Leon", "sdf123");
//获取最后访问的时间
print($user->getLastLogin() ."<br>\n");
//打印用户名
print("$user->name<br>\n");
?> <?php
//组件
class Widget
{
public $name='none';
public $created=FALSE;
}
//装配器
class Assembler
{
public function make(Widget $w)
{
print("Making $w->name<br>\n");
$w->created=TRUE;
}
}
//建立一个组件对象
$thing = new Widget;
$thing->name = 'Gadget';
//装配组件
Assembler::make($thing);
?> 注:本文章为原创文章,版权归文章作者与超越PHP网站所有,未经本站同意,禁止任何商业转载。非盈利网站及个人网站转载请注明出处,谢谢合作!