Interface ArrayAccess
Boolean offsetExists ($ index)
Mixed offsetGet ($ index)
Void offsetSet ($ index, $ newvalue)
Void offsetUnset ($ index)
The following example shows how to use this interface. The example is not complete, but can be understood.
<? 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 "Johns ID number is". $ userMap [John];
?>
In fact, when $ userMap [John] is searched and executed, PHP calls the offsetGet () method, which then calls the getUserId () method related to the database.