程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP之ArrayAccess接口實例代碼介紹

PHP之ArrayAccess接口實例代碼介紹

編輯:關於PHP編程

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

下面的例子展示了如何使用這個接口,例子並不是完整的,但是可以看懂。
<?php
class UserToSocialSecurity implements ArrayAccess
{
private $db;//一個包含著數據庫訪問方法的對象
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];
?>
實際上,當 $userMap[John] 查找被執行時,PHP 調用了 offsetGet() 方法,由這個方法再來調用數據庫相關的 getUserId() 方法。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved