PHP 的ArrayAccess接口 像数组一样来访问你的PHP对象
作者:bea
代码如下: interface ArrayAccess boolean offsetExists($index) mixed offsetGet($index) void offsetSet($index, $newvalue) void offsetUnset($index) 下面的例子展示了如何使用这个接口,例子并不是完整的,但是足够看懂,:-> 代码如下: <?php class UserToSocialSecurity imple
代码如下:
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 "John's ID number is " . $userMap['John'];
?>
实际上,当 $userMap['John'] 查找被执行时,PHP 调用了 offsetGet() 方法,由这个方法再来调用数据库相关的 getUserId() 方法。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
- 一篇有意思的技术文章php介绍篇
- 理解php原理的opcodes(操作码)
- PHP下常用正则表达式整理
- PHP编程最快明白(第一讲 软件环境和准备工作)
- 微盾PHP脚本加密专家php解密算法
- smarty中先strip_tags过滤html标签后truncate截取文章运用
- php正则过滤html标签、空格、换行符的代码(附说明)
- PHP 强制下载文件代码
- PHP 分页类代码(简单好用型)
- php下网站防IP攻击代码,超级实用
- php 实现进制转换(二进制、八进制、十六进制)互相转换实现代码
- php park、unpark、ord 函数使用方法(二进制流接口应用实例)
- php通过文件头检测文件类型通用代码类(zip,rar等)
- php empty,isset,is_null判断比较(差异与异同)
- 学习php笔记 字符串处理
- PHP 函数执行效率的小比较
- PHP类中Static方法效率测试代码
- 发款php蜘蛛统计插件只要有mysql就可用