Home > Backend Development > PHP Tutorial > PHP ArrayAccess interface example code introduction_PHP tutorial

PHP ArrayAccess interface example code introduction_PHP tutorial

WBOY
Release: 2016-07-13 17:41:59
Original
886 people have browsed it

interface ArrayAccess
boolean offsetExists($index)
mixed offsetGet($index)
void offsetSet($index, $newvalue)
void offsetUnset($index)

Example below Shows how to use this interface. The example is not complete, but it can be understood.
class UserToSocialSecurity implements ArrayAccess
{
private $db;//An object containing database access methods
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] search is performed, PHP calls the offsetGet() method, and then from this method Call the database-related getUserId() method.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486099.htmlTechArticleinterface ArrayAccess boolean offsetExists($index) mixed offsetGet($index) void offsetSet($index, $newvalue) void offsetUnset($index) The following example shows how to use this interface,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template