<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_62590"> <div class="blockcode"> <div id="code_AK7"><ol> <li> </li> <li><?php<li>header("Content-type:text/html; charset=utf-8");<li> <li>class Collection{<li> private $_members=array();<li> private $_onload;<li> private $_isLoaded=false;<li> <li> public function startLoad(){<li> $this->_checkCallback();</li> <li> }</li> <li> </li> <li> public function addItem($obj,$key=NULL){</li> <li> $this->_checkCallback();</li> <li> if($key){</li> <li> if(isset($this->_members[$key])){</li> <li> throw new Exception("Key \"$key\" already in use!");</li> <li> }else{</li> <li> $this->_members[$key]=$obj;</li> <li> }</li> <li> }else{</li> <li> $this->_members[]=$obj;</li> <li> }</li> <li> }</li> <li> </li> <li> public function removeItem($key){</li> <li> $this->_checkCallback();</li> <li> if(isset($this->_members[$key])){</li> <li> unset($this->_members[$key]);</li> <li> }else{</li> <li> throw new Exception("Invalid key \"$key\"!");</li> <li> }</li> <li> }</li> <li> </li> <li> public function getItem($key){</li> <li> $this->_checkCallback();</li> <li> if(isset($this->_members[$key])){</li> <li> return $this->_members[$key];</li> <li> }else{</li> <li> throw new Exception("Invalid key \"$key\"!");</li> <li> }</li> <li> }</li> <li> </li> <li> public function keys(){</li> <li> $this->_checkCallback();</li> <li> return array_keys($this->_members);</li> <li> }</li> <li> </li> <li> public function length(){</li> <li> $this->_checkCallback();</li> <li> return sizeof($this->_members);</li> <li> }</li> <li> </li> <li> public function exists($key){</li> <li> $this->_checkCallback();</li> <li> return (isset($this->_members[$key]));</li> <li> }</li> <li> </li> <li> public function setLoadCallback($functionName,$objOrClass=NULL){</li> <li> if($objOrClass){</li> <li> $callback=array($objOrClass,$functionName);</li> <li> }else{</li> <li> $callback=$functionName;</li> <li> }</li> <li> </li> <li> if(!is_callable($callback)){</li> <li> /*throw new Exception("$callableName is not callable"."as a parameter to oonload");*/</li> <li> echo "$callback is not callable<br>";</li> <li> return false;</li> <li> }</li> <li> </li> <li> $this->_onload=$callback;</li> <li> }</li> <li> </li> <li> private function _checkCallback(){</li> <li> if(isset($this->_onload)&&!$this->_isLoaded){</li> <li> $this->_isLoaded=true;</li> <li> call_user_func($this->_onload,$this);</li> <li> }</li> <li> }</li> <li>}</li> <li> </li> <li> </ol></div> <em onclick="copycode($('code_AK7'));">复制代码</em> </div> <br> </td></tr></table> <div class="ptg mbm mtn"> PHP, 컬렉션</div> <div id="comment_62590" class="cm"> </div> <div id="post_rate_div_62590"></div> <br><br>