Copy codeThe Code is 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. The example is not complete, but is understandable:->
Copy codeThe Code is 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'] is searched and executed, PHP calls the offsetGet () method, which then calls the getUserId () method related to the database.