Home >Backend Development >PHP Tutorial >PHP's ArrayAccess interface accesses your PHP objects like arrays_PHP Tutorial

PHP's ArrayAccess interface accesses your PHP objects like arrays_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:33:52920browse

Copy code The 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 it is enough to understand. :-> {
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 "John's ID number is " . $userMap['John'];
?>


In fact, when the $userMap['John'] search is executed, PHP calls the offsetGet() method, which then calls the database-related getUserId() method.



http://www.bkjia.com/PHPjc/322530.html

www.bkjia.com

true
http: //www.bkjia.com/PHPjc/322530.html
TechArticle

Copy the code as follows: interface ArrayAccess boolean offsetExists($index) mixed offsetGet($index) void offsetSet($index , $newvalue) void offsetUnset($index) The following example shows...
Statement:
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