Copy Code code as follows:
Interface Arrayaccess
Boolean offsetexists ($index)
Mixed Offsetget ($index)
void Offsetset ($index, $newvalue)
void Offsetunset ($index)
The following example shows how to use this interface, and the example is not complete, but it is enough to read,:->
Copy Code code as follows:
<?php
Class Usertosocialsecurity implements Arrayaccess
{
private $db;//An object that contains the database access method
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 '];
?>
In fact, when $userMap [' John '] lookup is executed, PHP invokes the Offsetget () method, which then invokes the database-related GetUserID () method.