好!欢迎访问迷津渡口 现在时间是: 天气 °C ~ °C

迷津渡口

越感到迷惑 越接近真理

PHP类实现ArrayAccess接口

什么是SPL?

SPL是Standard PHP Library的缩写,属于PHP标准库的一支,是PHP5最新增加的功能,用来解决一些PHP语言本身不支持的问题以及实现更有效的数据的操作。

数组接口ArrayAccess--赋值、取值、删除、判断是否为空

数组的特性非常强大,如果对象可以使用数组的特性,将非常方便。

使用的方法类似于PHP5的魔术方法,对象通过定义相应的魔术方法可以实现一个数组接口,从而使用数组的特性。

需要定义的四种魔术方法:

boolean offsetExists($index)        //调用数组判断是否配置索引isset方法
mixed offsetGet($index)             //使用数组索引读取方法
void offsetSet($index, $newvalue)   //使用数组索引赋值方法
void offsetUnset($index)            //使用数组删除索引和值unset方法

使用例子:

<?php
/*
* 测试ArrayAccess接口
* echo __METHOD__."\n";调用时输出调用方法,方便跟踪
*
*/

class FakeArray implements ArrayAccess {
	private $elements;

	public function __construct() {
		echo __METHOD__."\n";
		$this->elements=array();
	}

	public function offsetExists($offset) {
		echo __METHOD__."\n";
		return isset($this->elements[$offset]);
	}

	public function offsetGet($offset) {
		echo __METHOD__."\n";
		return $this->elements[$offset];
	}

	public function offsetSet($offset,$value) {
		echo __METHOD__."\n";
		$this->elements[$offset]=$value;
	}

	public function offsetUnset($offset) {
		echo __METHOD__."\n";
		unset($this->elements[$offset]);
	}
}

//使用
$exam=new FakeArray;
$exam['a']='test';

if (isset($exam['a']) && $exam['a']=='test') {
	echo "\$exam['a'] is set as 'test' now.\n";
}

unset($exam['a']);

if (! isset($exam['a'])) {
	echo "\$exam['a'] is unset now.\n";
}
?>


点赞0
 标签: php ArrayAccess
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
snifferwalk
 snifferwalk 约 9 小时前回复
#226
<a href="http://www.adanastarpen.com/adidas-neo-honeycomb-oransje-svart-obuvl">adidas neo honeycomb oransje svart</a> <a href="http://www.actconfident.com/air-jordan-retro-6-green-red-shoesh">air jordan retro 6 green red</a> <a href="http://www.erzurumihl.com/nike-roshe-blu-leopard-scarpei">nike roshe blu leopard</a> <a href="http://www.candctowingil.com/herren-air-max-90-em-lila-wei%C3%9F-schuhej">herren air max 90 em lila wei脽</a> <a href="http://www.onlysidekick.com/vintage-boston-celtics-hat-hath">vintage boston celtics hat</a> <a href="http://www.rmmassage.com/nike-air-max-1-femmes-violet-rose-chaussuresh">nike air max 1 femmes violet rose</a>
snifferwalk http://www.snifferwalk.com/
mamatale
 mamatale 约 15 小时前回复
#225
<a href="http://www.dbievents.com/adidas-originals-shoes-green-shoesj">adidas originals shoes green</a> <a href="http://www.ilankocaeli.com/new-era-wooly-hats-hatk">new era wooly hats</a> <a href="http://www.msignworx.com/herre-nike-air-max-95-bl%C3%A5-r%C3%B8d-skoj">herre nike air max 95 bl氓 r酶d</a> <a href="http://www.dianegrams.com/ralph-lauren-woven-bag-poloh">ralph lauren woven bag</a> <a href="http://www.jobstodayng.com/adidas-superstar-black-leopard-shoesi">adidas superstar black leopard</a> <a href="http://www.hardcoreboris.com/adidas-originals-eqt-support-adv-grigio-scarpej">adidas originals eqt support adv grigio</a>
mamatale http://www.mamatale.com/
sportablecrew
 sportablecrew 2019-05-18 17:34回复
#224
<a href="http://www.mwartsdrama.com/air-jordan-retro-11-femminile-rosa-arancia-scarpek">air jordan retro 11 femminile rosa arancia</a> <a href="http://www.meshmotors.com/nike-huarache-ultra-noir-blanc-chaussuresh">nike huarache ultra noir blanc</a> <a href="http://www.aspenmedisys.com/prada-ladies-sunglasses-pradah">prada ladies sunglasses</a> <a href="http://www.smartiesland.com/chicago-blackhawks-88-patrick-kane-black-ice-skulls-jersey-nfli">chicago blackhawks 88 patrick kane black ice skulls jersey</a> <a href="http://www.emcetolerie.com/herre-asics-gel-noosa-tri-9-s%C3%B8lv-gr%C3%B8n-skol">herre asics gel noosa tri 9 s酶lv gr酶n</a> <a href="http://www.lapaktoko.com/ralph-lauren-black-pullover-hoodie-poloh">ralph lauren black pullover hoodie</a>
sportablecrew http://www.sportablecrew.com/
texarillo
 texarillo 2019-05-18 16:50回复
#223
<a href="http://www.colintilley.com/nike-air-force-1-storm-bl%C3%A5-vivid-oransje-obuvi">nike air force 1 storm bl氓 vivid oransje</a> <a href="http://www.xthbgc.com/nike-zoom-structure-20-kvinners-gr%C3%A5-r%C3%B8d-obuvp">nike zoom structure 20 kvinners gr氓 r酶d</a> <a href="http://www.iberishop.com/nike-lebron-ambassador-9-gull-himmelen-bl%C3%A5-obuvi">nike lebron ambassador 9 gull himmelen bl氓</a> <a href="http://www.karmacanix.com/nike-air-griffey-max-1-rojo-zapatosp">nike air griffey max 1 rojo</a> <a href="http://www.tobepartridge.com/coach-op-art-large-yellow-wallets-coachh">coach op art large yellow wallets</a> <a href="http://www.shopdunks.com/air-jordan-retro-28-blue-yellow-shoesj">air jordan retro 28 blue yellow</a>
[url=http://www.texarillo.com/]texarillo[/url]
ozgurankara
 ozgurankara 2019-05-16 20:20回复
#222
<a href="http://www.bikashskma.com/kvinners-nike-air-max-1-r%C3%B8d-obuvi">kvinners nike air max 1 r酶d</a> <a href="http://www.videoaulaneri.com/bright-viola-converse-scarpek">bright viola converse</a> <a href="http://www.yemekizmit.com/mens-adidas-predator-boots-shoesi">mens adidas predator boots</a> <a href="http://www.drlforbes.com/nike-lebron-15-azul-zapatosi">nike lebron 15 azul</a> <a href="http://www.siglosgourmet.com/sort-friday-adidas-tubular-skoh">sort friday adidas tubular</a> <a href="http://www.cadeemy.com/maschio-adidas-nmd-runner-arancia-scarpei">maschio adidas nmd runner arancia</a>
ozgurankara http://www.ozgurankara.com/
procrastivity
 procrastivity 2019-05-16 11:03回复
#221
<a href="http://www.htpmimoza.com/nike-kaishi-schwarz-and-gold-schuhej">nike kaishi schwarz and gold</a> <a href="http://www.ayakkabiyeri.com/new-jersey-devils-adidas-nhl-respect-snapback-cap-hath">new jersey devils adidas nhl respect snapback cap</a> <a href="http://www.sozoteki.com/hommes-nike-zoom-winflo-2-noir-carbon-gris-chaussuresh">hommes nike zoom winflo 2 noir carbon gris</a> <a href="http://www.njforward.com/nike-hypervenom-2-orange-and-schwarz-schuheh">nike hypervenom 2 orange and schwarz</a> <a href="http://www.crimediainc.com/nike-mercurial-superfly-iv-bhm-tf-black-white-shoesk">nike mercurial superfly iv bhm tf black white</a> <a href="http://www.claybrookpenn.com/adidas-originals-samba-super-gr%C3%B8n-skoj">adidas originals samba super gr酶n</a>
procrastivity http://www.procrastivity.com/
billige b酶rneting
 billige b酶rneting 2019-05-16 10:27回复
#220
<a href="http://www.naijahope.com/both/sand%c3%a1-ia-verniz-preta-arezzo">sand谩 ia verniz preta arezzo</a><a href="http://www.naijahope.com/break/tenis-mascu-ino-skate-freeday-f-ip-eco-todo-preto-origina-.-carregando">tenis mascu ino skate freeday f ip eco todo preto origina . carregando</a><a href="http://www.naijahope.com/buyer/bo-sa-mate-asse-esti-o-chane-ombro-yra">bo sa mate asse esti o chane ombro yra</a><a href="http://www.naijahope.com/can/grande-chane-coco-grained-agne-o-nero-con-nero-manico">grande chane coco grained agne o nero con nero manico</a>
billige b酶rneting http://www.tigernewsindia.com/mouse/billige-b%c3%b8rneting
irstdatesgame
 irstdatesgame 2019-05-16 01:48回复
#219
<a href="http://www.gibbsorganic.com/nike-air-pegasus-92-jaune-vert-kurpesd">nike air pegasus 92 jaune vert</a> <a href="http://www.malatyapi.com/nike-kyrie-1-wei%C3%9F-schuhel">nike kyrie 1 wei脽</a> <a href="http://www.calwhitson.com/air-jordan-retro-9-orange-brot-skof">air jordan retro 9 orange brot</a> <a href="http://www.glamourspausa.com/nike-roshe-two-gul-sort-footf">nike roshe two gul sort</a> <a href="http://www.thebeadpro.com/air-jordan-retro-10-royal-blu-viola-sneakerd">air jordan retro 10 royal blu viola</a> <a href="http://www.mollynurse.com/new-balance-574-argento-verde-schuhef">new balance 574 argento verde</a>
irstdatesgame http://www.irstdatesgame.com/
keleric
 keleric 2019-05-15 18:55回复
#218
<a href="http://www.teamdalarna.com/nike-air-max-95-dam%C3%A4nner-orangen-rosa-schuheh">nike air max 95 dam盲nner orangen rosa</a> <a href="http://www.syairshamyoun.com/mennns-nike-air-huarache-nm-r%C3%B8d-lilla-obuvj">mennns nike air huarache nm r酶d lilla</a> <a href="http://www.fizalisfresh.com/nike-sb-stefan-janoski-max-rosa-cielo-blu-scarpek">nike sb stefan janoski max rosa cielo blu</a> <a href="http://www.fsscas.com/nike-free-rn-flyknit-noir-friday-chaussuresh">nike free rn flyknit noir friday</a> <a href="http://www.imagimp.com/prada-double-bag-sizes-pradah">prada double bag sizes</a> <a href="http://www.burmamaritime.com/throwback-julio-jones-jersey-nfli">throwback julio jones jersey</a>
keleric http://www.keleric.com/
macusaa
 macusaa 2019-05-14 12:56回复
#217
<a href="http://www.kaliteankara.com/girls-light-bleu-converse-chaussuresj">girls light bleu converse</a> <a href="http://www.bagsbeat.com/dam%C3%A4nner-schwarz-leder-converse-hi-tops-schuhek">dam盲nner schwarz leder converse hi tops</a> <a href="http://www.lolalolincoln.com/john-wall-christmas-day-jersey-nflh">john wall christmas day jersey</a> <a href="http://www.buycharboleps.com/nike-free-tr-menn-dark-bl%C3%A5-obuvi">nike free tr menn dark bl氓</a> <a href="http://www.androidsmith.com/pink-high-top-converse-toddler-shoesj">pink high top converse toddler</a> <a href="http://www.oheyewear.com/women-air-jordan-retro-4-white-grey-purple-shoesl">women air jordan retro 4 white grey purple</a>
macusaa http://www.macusaa.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸