好!欢迎访问迷津渡口 现在时间是: 天气 °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
   支持一下
挤眼 亲亲 咆哮 开心 想想 可怜 糗大了 委屈 哈哈 小声点 右哼哼 左哼哼 疑问 坏笑 赚钱啦 悲伤 耍酷 勾引 厉害 握手 耶 嘻嘻 害羞 鼓掌 馋嘴 抓狂 抱抱 围观 威武 给力
 点赞
 签到
 表情
 图片
 代码
提交评论

清空信息
关闭评论
bacaiptek
 bacaiptek 约 18 小时前回复
#107
<a href="http://www.matrawildlife.com/buy-ray-ban-justin-sunglassesh">buy ray ban justin</a> <a href="http://www.hiyawigs.com/noir-vert-hommes-air-jordan-31-chaussures-chaussuresl">noir vert hommes air jordan 31 chaussures</a> <a href="http://www.fidgefingers.com/tout-blanc-converse-trainers-chaussuresl">tout blanc converse trainers</a> <a href="http://www.lintvan.com/alle-wei%C3%9F-nike-shox-herren-schuhej">alle wei脽 nike shox herren</a> <a href="http://www.pubgdust.com/adidas-ace-15.1-turf-gold-wei%C3%9F-schwarz-schuhei">adidas ace 15.1 turf gold wei脽 schwarz</a> <a href="http://www.ahtantalya.com/air-jordan-retro-13-svart-hvit-rosa-obuvj">air jordan retro 13 svart hvit rosa</a>
bacaiptek http://www.bacaiptek.com/
cooterscamera
 cooterscamera 2019-03-19 12:24回复
#106
<a href="http://www.atrapaniapp.com/air-jordan-retro-31-rosa-himmelblau-schuhej">air jordan retro 31 rosa himmelblau</a> <a href="http://www.maplebeyond.com/virginia-tech-hokies-top-of-the-world-ncaa-society-adjustable-cap-hath">virginia tech hokies top of the world ncaa society adjustable cap</a> <a href="http://www.collisionsix.com/hommes-nike-free-run-3-oranger-gris-chaussuresh">hommes nike free run 3 oranger gris</a> <a href="http://www.todololitas.com/femminile-nike-roshe-run-triangle-nero-rosso-scarpeq">femminile nike roshe run triangle nero rosso</a> <a href="http://www.jcwjrlaw.com/nike-roshe-run-black-grey-kaskus-shoesj">nike roshe run black grey kaskus</a> <a href="http://www.vstsblog.com/air-jordan-retro-11-sort-lyser%C3%B8d-skoi">air jordan retro 11 sort lyser酶d</a>
cooterscamera http://www.cooterscamera.com/
oqibo
 oqibo 2019-03-17 09:44回复
#105
<a href="http://www.redlineart.net/nike-air-max-ltd-2-maschio-in-esecuzione-scarpa-obuva">nike air max ltd 2 maschio in esecuzione scarpa</a> <a href="http://www.reedrummond.net/lebron-soldier-10-all-r%C3%B8d-jordans-obuve">lebron soldier 10 all r酶d jordans</a> <a href="http://www.regulationonline.net/air-max-90-hvit-jd-sports-obuvf">air max 90 hvit jd sports</a> <a href="http://www.reporterviaggi.net/nike-air-huarache-matte-plata-aviator-schuhef">nike air huarache matte plata aviator</a> <a href="http://www.restoringthechurch.net/nike-sb-stefan-janoski-max-mujeres-negro-cielo-azul-kurpesd">nike sb stefan janoski max mujeres negro cielo azul</a> <a href="http://www.rusticlighting.net/air-max-zero-qs-silber-key-butyf">air max zero qs silber key</a>
oqibo http://www.oqibo.net/
atientertainment
 atientertainment 2019-03-17 08:55回复
#104
<a href="http://www.caninewingman.net/air-jordan-retro-7-french-bl%C3%A5-obuva">air jordan retro 7 french bl氓</a> <a href="http://www.caqueta.net/kd-trey-5-gr%C3%A5-and-oransje-air-max-obuve">kd trey 5 gr氓 and oransje air max</a> <a href="http://www.clubcombo.net/nike-internationalist-gr%C3%BCn-herren-obuvf">nike internationalist gr眉n herren</a> <a href="http://www.cometwifi.net/nike-hyperdunk-basketballe-sko-2015-kvinders-schuhef">nike hyperdunk basketballe sko 2015 kvinders</a> <a href="http://www.communityfm.net/nike-air-force-1-bianca-hot-kurpesd">nike air force 1 bianca hot</a> <a href="http://www.coralbayconsulting.net/nike-free-flyknit-chukka-rosa-gr%C3%BCn-butyf">nike free flyknit chukka rosa gr眉n</a>
atientertainment http://www.atientertainment.net/
webrushllc
 webrushllc 2019-03-17 03:12回复
#103
<a href="http://www.townyplay.com/nike-air-max-90-boot-ns-mujer-negro-verde-zapatosh">nike air max 90 boot ns mujer negro verde</a> <a href="http://www.djpunjabii.com/adidas-gazelle-vapour-lyser%C3%B8d-hvid-guld-metalleic-skok">adidas gazelle vapour lyser酶d hvid guld metalleic</a> <a href="http://www.avsomurie.com/adidas-superstar-classic-hvit-hologram-iridescent-obuvi">adidas superstar classic hvit hologram iridescent</a> <a href="http://www.circusveil.com/nike-mercurial-cr7-blanco-zapatosl">nike mercurial cr7 blanco</a> <a href="http://www.laconiaoil.com/oakley-minute-sunglasses-brown-gray-sunglassesh">oakley minute sunglasses brown gray</a> <a href="http://www.hurizmir.com/dope-apollo-snapback-hatj">dope apollo snapback</a>
webrushllc http://www.webrushllc.com/
netcarbdiet
 netcarbdiet 2019-03-17 02:06回复
#102
<a href="http://www.dorinsanat.com/nike-youth-jameis-winston-elite-green-jersey-tampa-bay-buccaneers-nfl-3-salute-to-service-nflx">nike youth jameis winston elite green jersey tampa bay buccaneers nfl 3 salute to service</a> <a href="http://www.hieuphamdmd.com/nike-sb-stefan-janoski-max-wei%C3%9F-junior-kurpesc">nike sb stefan janoski max wei脽 junior</a> <a href="http://www.pascaltosi.com/nike-elite-richard-sherman-jersey-25-nfl-seattle-seahawks-rush-vapor-untouchable-youth-green-nflx">nike elite richard sherman jersey 25 nfl seattle seahawks rush vapor untouchable youth green</a> <a href="http://www.sethba.com/nike-vomero-herre-l%C3%B8b-sko-uk-obuvd">nike vomero herre l酶b sko uk</a> <a href="http://www.stevenrobins.com/nike-free-5.0-run-2014-oransje-svart-sko-scarpec">nike free 5.0 run 2014 oransje svart sko</a> <a href="http://www.syrosmusic.com/air-jordan-super-fly-iv-gold-grau-kurpesb">air jordan super fly iv gold grau</a>
[url=http://www.netcarbdiet.com/]netcarbdiet[/url]
blakefanz
 blakefanz 2019-03-16 14:04回复
#101
<a href="http://www.kaliteankara.com/gr%C3%B8n-converse-chucks-skok">gr酶n converse chucks</a> <a href="http://www.bagsbeat.com/m%C3%A4nner-nike-air-huarache-marine-flat-opal-wei%C3%9F-island-gr%C3%BCn-schuheh">m盲nner nike air huarache marine flat opal wei脽 island gr眉n</a> <a href="http://www.lolalolincoln.com/nike-flyknit-racer-oreo-black-shoesk">nike flyknit racer oreo black</a> <a href="http://www.buycharboleps.com/nike-air-force-1-high-sort-gum-skol">nike air force 1 high sort gum</a> <a href="http://www.androidsmith.com/mennns-nike-shox-nz-sko-svart-gull-gul-online-obuvk">mennns nike shox nz sko svart gull gul online</a> <a href="http://www.oheyewear.com/nike-lunarepic-low-flyknit-grigio-scarpei">nike lunarepic low flyknit grigio</a>
blakefanz http://www.blakefanz.com/
maletaboricua
 maletaboricua 2019-03-16 02:12回复
#100
<a href="http://www.bharatovation.com/ray-ban-flash-lenses-cheap-sunglassesr">ray ban flash lenses cheap</a> <a href="http://www.charleyscafe.com/reebok-gl-6000-white-shoesv">reebok gl 6000 white</a> <a href="http://www.hopingforsnow.com/nike-black-white-huarache-skow">nike black white huarache</a> <a href="http://www.housemykonos.com/mackage-amalya-coat-mackager">mackage amalya coat</a> <a href="http://www.maxfullforce.com/billig-nike-air-max-90-essential-dam%C3%A4nner-gold-skod">billig nike air max 90 essential dam盲nner gold</a> <a href="http://www.guntruthers.com/nike-houston-texans-89-stephen-anderson-limited-navy-blue-player-name-number-tank-top-jersey-nflc">nike houston texans 89 stephen anderson limited navy blue player name number tank top jersey</a>
maletaboricua http://www.maletaboricua.com/
eisenhards
 eisenhards 2019-03-15 21:03回复
#99
<a href="http://www.matrawildlife.com/adidas-nitrocharge-1.0-fg-negro-pack-zapatosk">adidas nitrocharge 1.0 fg negro pack</a> <a href="http://www.hiyawigs.com/nike-roshe-run-white-black-gold-shoesj">nike roshe run white black gold</a> <a href="http://www.fidgefingers.com/arizona-coyotes-snapback-hath">arizona coyotes snapback</a> <a href="http://www.lintvan.com/mujeres-air-jordan-retro-13-blanco-rosado-zapatosl">mujeres air jordan retro 13 blanco rosado</a> <a href="http://www.pubgdust.com/adidas-neo-mesh-white-sneaker-shoes-shoesk">adidas neo mesh white sneaker shoes</a> <a href="http://www.ahtantalya.com/menn-nike-lebron-12-oransje-r%C3%B8d-gul-obuvi">menn nike lebron 12 oransje r酶d gul</a>
eisenhards http://www.eisenhards.com/
emcetolerie
 emcetolerie 2019-03-15 09:09回复
#98
<a href="http://www.teamdalarna.com/oakley-medicine-hat-hath">oakley medicine hat</a> <a href="http://www.syairshamyoun.com/femmes-nike-kobe-9-low-rouge-bleu-chaussuresh">femmes nike kobe 9 low rouge bleu</a> <a href="http://www.fizalisfresh.com/maschio-nike-free-tr-gituttio-arancia-scarpel">maschio nike free tr gituttio arancia</a> <a href="http://www.fsscas.com/womens-nike-air-max-tailwind-8-green-gold-shoesj">womens nike air max tailwind 8 green gold</a> <a href="http://www.imagimp.com/air-jordan-12-light-lyser%C3%B8d-skoi">air jordan 12 light lyser酶d</a> <a href="http://www.burmamaritime.com/femminile-air-jordan-retro-4-bianca-grigio-scarpeq">femminile air jordan retro 4 bianca grigio</a>
emcetolerie http://www.emcetolerie.com/
顶部留言底部
 00:00/00:00
我想和你虚度时光 - 花房姑娘(9)
  1. 我想和你虚度时光
  2. 花房姑娘
  3. 恋恋风尘
  4. 我喜爱一切不彻底的事物
  5. 我的心里是满的
  6. Long Way
  7. 给少年的歌
  8. 晴日共剪窗
  9. 天上的月你的脸