PHP 的ArrayAccess接口 像数组一样来访问你的PHP对象
<div class="codetitle"><a style="CURSOR: pointer" data="17647" class="copybut" id="copybut17647" onclick="doCopy('code17647')"> 代码如下:
<div class="codebody" id="code17647">
interface ArrayAccess
boolean offsetExists($index)
mixed offsetGet($index)
void offsetSet($index,$newvalue)
void offsetUnset($index)
下面的例子展示了如何使用这个接口,例子并不是完整的,但是足够看懂,:->
<div class="codetitle">
<a style="CURSOR: pointer" data="9361" class="copybut" id="copybut9361" onclick="doCopy('code9361')"> 代码如下: <div class="codebody" id="code9361">
<?
PHP class UserToSocialSecurity implements ArrayAccess
{
private $db;//一个包含着
数据库访问
方法的对象
function offsetExists($name)
{
return $this->db->userExists($name);
}
function offsetGet($name)
{
return $this->db->getUserId($name);
}
function offsetSet($name,$id)
{
$this->db->setUserId($name,$id);
}
function offsetUnset($name)
{
$this->db->removeUser($name);
}
}
$userMap = new UserToSocialSecurity();
print "John's ID number is " . $userMap['John'];
?>