CList
鍖 | system.collections |
---|---|
缁ф壙 | class CList » CComponent |
瀹炵幇 | IteratorAggregate, Traversable, ArrayAccess, Countable |
瀛愮被 | CFilterChain, CTypedList |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CList.php 3430 2011-11-02 23:10:03Z alexander.makarow@gmail.com $ |
婧愮爜 | framework/collections/CList.php |
浣犲彲浠ラ氳繃浣跨敤 itemAt锛add锛insertAt锛remove锛屼互鍙removeAt璁块棶锛屾坊鍔狅紝鎻掑叆锛屽垹闄ら」鐩 閫氳繃getCount锛屼綘鍙互鍙栧緱杩欎釜鍒楄〃鐨勯」鐩暟銆 濡備笅鎵绀猴紝CList涔熷彲浠ュ綋浣滀竴鑸暟缁勬潵浣跨敤锛
$list[]=$item; //鍦ㄥ熬閮ㄦ坊鍔 $list[$index]=$item; //$index涓瀹氳鍦0鍜$list->Count涔嬮棿 unset($list[$index]); //鍒犻櫎鎸囧畾$index鐨勯」鐩 if(isset($list[$index])) //濡傛灉鍒楄〃涓瓨鍦$index鎸囧畾鐨勯」鐩 foreach($list as $index=>$item) //閬嶅巻鍒楄〃涓殑椤圭洰 $n=count($list); //杩斿洖鍒楄〃涓殑椤圭洰鏁
閫氳繃閲嶅啓insertAt()锛屼互涓removeAt()To extend CList by doing additional operations with each addition or removal 浣犲彲浠ュ湪鎷ユ湁娣诲姞/鍒犻櫎鎿嶄綔鐨勫熀纭涓婃墿灞曢澶栫殑鎿嶄綔锛堝绫诲瀷妫鏌ワ級銆
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
count | integer | 杩斿洖鍒楄〃鐨勯」鐩暟銆 | CList |
iterator | Iterator | 杩斿洖閬嶅巻杩欎釜椤圭洰鍒楄〃鐨勮凯浠e櫒銆 | CList |
readOnly | boolean | 杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆 | CList |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__call() | 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 | CComponent |
__construct() | 鏋勯犳柟娉曘 | CList |
__get() | 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 | CComponent |
__isset() | 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 | CComponent |
__set() | 璁剧疆涓涓粍浠剁殑灞炴у笺 | CComponent |
__unset() | 璁剧疆涓涓粍浠剁殑灞炴т负null銆 | CComponent |
add() | 鍦ㄥ垪琛ㄥ熬閮ㄦ坊鍔犱竴涓」鐩 | CList |
asa() | 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° | CComponent |
attachBehavior() | 闄勫姞涓涓涓哄埌缁勪欢銆 | CComponent |
attachBehaviors() | 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 | CComponent |
attachEventHandler() | 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 | CComponent |
canGetProperty() | 纭畾灞炴ф槸鍚﹀彲璇汇 | CComponent |
canSetProperty() | 纭畾灞炴ф槸鍚﹀彲鍐欍 | CComponent |
clear() | 鍒犻櫎鍒楄〃涓墍鏈夐」鐩 | CList |
contains() | CList | |
copyFrom() | 灏嗚凯浠e櫒涓殑鏁版嵁澶嶅埗鍒板垪琛ㄣ | CList |
count() | 杩斿洖鍒楄〃鐨勯」鐩暟銆 | CList |
detachBehavior() | 浠庣粍浠朵腑鍒嗙涓涓涓恒 | CComponent |
detachBehaviors() | 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 | CComponent |
detachEventHandler() | 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 | CComponent |
disableBehavior() | 绂佺敤涓涓檮鍔犺涓恒 | CComponent |
disableBehaviors() | 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
enableBehavior() | 鍚敤涓涓檮鍔犺涓恒 | CComponent |
enableBehaviors() | 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
evaluateExpression() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
getCount() | 杩斿洖鍒楄〃鐨勯」鐩暟銆 | CList |
getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
getIterator() | 杩斿洖閬嶅巻杩欎釜椤圭洰鍒楄〃鐨勮凯浠e櫒銆 | CList |
getReadOnly() | 杩斿洖杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆 | CList |
hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
indexOf() | CList | |
insertAt() | 灏嗛」鐩彃鍏ュ埌鎸囧畾浣嶇疆銆 | CList |
itemAt() | 杩斿洖鎸囧畾浣嶇疆鐨勯」鐩 | CList |
mergeWith() | 灏嗚凯浠e櫒鐨勬暟鎹暣鍚堝埌map | CList |
offsetExists() | 杩斿洖鍊艰鏄庡垪琛ㄤ腑鏄惁鍖呭惈杩欎釜椤圭洰銆 | CList |
offsetGet() | 杩斿洖鎸囧畾浣嶇疆鐨勯」鐩 | CList |
offsetSet() | 鍦ㄦ寚瀹氫綅缃彃鍏ラ」鐩 | CList |
offsetUnset() | 鍒犻櫎鎸囧畾浣嶇疆鐨勯」鐩 | CList |
raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
remove() | 浠庡垪琛ㄤ腑鍒犻櫎涓涓」鐩 | CList |
removeAt() | 鍒犻櫎鎸囧畾浣嶇疆鐨勯」鐩 | CList |
toArray() | CList |
灞炴ц缁
杩斿洖鍒楄〃鐨勯」鐩暟銆
杩斿洖閬嶅巻杩欎釜椤圭洰鍒楄〃鐨勮凯浠e櫒銆 姝ゆ柟娉曚负鎺ュ彛IteratorAggregate寮哄埗瑕佹眰瀹炵幇銆
杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆
鏂规硶璇︾粏
public void __construct(array $data=NULL, boolean $readOnly=false)
| ||
$data | array | 鍒濆鍖栫殑鏁版嵁銆傞粯璁や负null锛屾剰鍛崇潃娌℃湁鍒濆鍖栥 |
$readOnly | boolean | 鏍囪瘑杩欎釜鍒楄〃鏄惁涓哄彧璇汇 |
public function __construct($data=null,$readOnly=false)
{
if($data!==null)
$this->copyFrom($data);
$this->setReadOnly($readOnly);
}
鏋勯犳柟娉曘 鏍规嵁鏁扮粍鎴栬呰凯浠e璞″垵濮嬪寲杩欎釜鍒楄〃銆
public integer add(mixed $item)
| ||
$item | mixed | 鏂扮殑椤圭洰 |
{return} | integer | 杩斿洖鍦ㄥ姞鍏ヨ椤圭洰鐨勪粠闆跺紑濮嬬殑绱㈠紩 |
public function add($item)
{
$this->insertAt($this->_c,$item);
return $this->_c-1;
}
鍦ㄥ垪琛ㄥ熬閮ㄦ坊鍔犱竴涓」鐩
public void clear()
|
public function clear()
{
for($i=$this->_c-1;$i>=0;--$i)
$this->removeAt($i);
}
鍒犻櫎鍒楄〃涓墍鏈夐」鐩
public boolean contains(mixed $item)
| ||
$item | mixed | 椤圭洰瀵硅薄 |
{return} | boolean | 杩斿洖鍊艰鏄庡垪琛ㄤ腑鏄惁鍖呭惈杩欎釜椤圭洰銆 |
public function contains($item)
{
return $this->indexOf($item)>=0;
}
public void copyFrom(mixed $data)
| ||
$data | mixed | 瑕佸鍒剁殑鏁版嵁, 鍙兘鏄暟缁勬垨鑰呯户鎵夸簬Traversable鐨勫璞° |
public function copyFrom($data)
{
if(is_array($data) || ($data instanceof Traversable))
{
if($this->_c>0)
$this->clear();
if($data instanceof CList)
$data=$data->_d;
foreach($data as $item)
$this->add($item);
}
else if($data!==null)
throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.'));
}
灏嗚凯浠e櫒涓殑鏁版嵁澶嶅埗鍒板垪琛ㄣ 娉ㄦ剰锛屽垪琛ㄤ腑宸茬粡瀛樺湪鐨勬暟鎹細琚鍏堝垹闄ゃ
public integer count()
| ||
{return} | integer | 杩斿洖鍒楄〃鐨勯」鐩暟銆 |
杩斿洖鍒楄〃鐨勯」鐩暟銆 姝ゆ柟娉曚负鎺ュ彛Countable寮哄埗瑕佹眰瀹炵幇銆
public integer getCount()
| ||
{return} | integer | 杩斿洖鍒楄〃鐨勯」鐩暟 |
杩斿洖鍒楄〃鐨勯」鐩暟銆
public Iterator getIterator()
| ||
{return} | Iterator | 杩斿洖鐢ㄦ潵閬嶅巻鍒楄〃涓殑椤圭洰鐨勮凯浠e櫒銆 |
public function getIterator()
{
return new CListIterator($this->_d);
}
杩斿洖閬嶅巻杩欎釜椤圭洰鍒楄〃鐨勮凯浠e櫒銆 姝ゆ柟娉曚负鎺ュ彛IteratorAggregate寮哄埗瑕佹眰瀹炵幇銆
public boolean getReadOnly()
| ||
{return} | boolean | 杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆 |
public integer indexOf(mixed $item)
| ||
$item | mixed | 椤圭洰瀵硅薄 |
{return} | integer | 杩斿洖椤圭洰鍦ㄥ垪琛ㄤ腑鐨勭储寮曞硷紙鍩轰簬锛愶級锛-1琛ㄧず娌℃湁鎵惧埌銆 |
public function indexOf($item)
{
if(($index=array_search($item,$this->_d,true))!==false)
return $index;
else
return -1;
}
public void insertAt(integer $index, mixed $item)
| ||
$index | integer | 鎸囧畾浣嶇疆銆 |
$item | mixed | 鏂扮殑椤圭洰 |
public function insertAt($index,$item)
{
if(!$this->_r)
{
if($index===$this->_c)
$this->_d[$this->_c++]=$item;
else if($index>=0 && $index<$this->_c)
{
array_splice($this->_d,$index,0,array($item));
$this->_c++;
}
else
throw new CException(Yii::t('yii','List index "{index}" is out of bound.',
array('{index}'=>$index)));
}
else
throw new CException(Yii::t('yii','The list is read only.'));
}
灏嗛」鐩彃鍏ュ埌鎸囧畾浣嶇疆銆 璇ヤ綅缃殑鍘熼」鐩拰 瀹冨悗闈㈢洿鍒板垪琛ㄧ粨鏉熺殑椤圭洰浼氳鍒犻櫎銆
public mixed itemAt(integer $index)
| ||
$index | integer | 椤圭洰鐨勭储寮曞 |
{return} | mixed | 绱㈠紩瀵瑰簲鐨勯」鐩 |
public function itemAt($index)
{
if(isset($this->_d[$index]))
return $this->_d[$index];
else if($index>=0 && $index<$this->_c) // in case the value is null
return $this->_d[$index];
else
throw new CException(Yii::t('yii','List index "{index}" is out of bound.',
array('{index}'=>$index)));
}
杩斿洖鎸囧畾浣嶇疆鐨勯」鐩 杩欎釜鏂规硶璺offsetGet鏄畬鍏ㄤ竴鏍风殑銆
public void mergeWith(mixed $data)
| ||
$data | mixed | 瑕佸悎骞剁殑鏁版嵁, 鍙兘鏄暟缁勬垨鑰呯户鎵夸簬Traversable鐨勫璞. |
public function mergeWith($data)
{
if(is_array($data) || ($data instanceof Traversable))
{
if($data instanceof CList)
$data=$data->_d;
foreach($data as $item)
$this->add($item);
}
else if($data!==null)
throw new CException(Yii::t('yii','List data must be an array or an object implementing Traversable.'));
}
灏嗚凯浠e櫒鐨勬暟鎹暣鍚堝埌map 鏂扮殑鏁版嵁浼氭坊鍔犲埌宸插瓨鍦ㄧ殑鏁版嵁鍚庨潰銆
public boolean offsetExists(integer $offset)
| ||
$offset | integer | 瑕佹鏌ョ殑浣嶇疆 |
{return} | boolean |
public function offsetExists($offset)
{
return ($offset>=0 && $offset<$this->_c);
}
杩斿洖鍊艰鏄庡垪琛ㄤ腑鏄惁鍖呭惈杩欎釜椤圭洰銆 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public mixed offsetGet(integer $offset)
| ||
$offset | integer | 瑕佹彃鍏ラ」鐩殑绱㈠紩銆 |
{return} | mixed | 杩斿洖璇ョ储寮曠殑椤圭洰銆 |
public function offsetGet($offset)
{
return $this->itemAt($offset);
}
杩斿洖鎸囧畾浣嶇疆鐨勯」鐩 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public void offsetSet(integer $offset, mixed $item)
| ||
$offset | integer | 瑕佽缃」鐩殑浣嶇疆 |
$item | mixed | 椤圭洰瀵硅薄 |
public function offsetSet($offset,$item)
{
if($offset===null || $offset===$this->_c)
$this->insertAt($this->_c,$item);
else
{
$this->removeAt($offset);
$this->insertAt($offset,$item);
}
}
鍦ㄦ寚瀹氫綅缃彃鍏ラ」鐩 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public void offsetUnset(integer $offset)
| ||
$offset | integer | 瑕佸垹闄ょ殑椤圭洰銆 |
public function offsetUnset($offset)
{
$this->removeAt($offset);
}
鍒犻櫎鎸囧畾浣嶇疆鐨勯」鐩 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public integer remove(mixed $item)
| ||
$item | mixed | 瑕佸垹闄ょ殑椤圭洰鐨勭储寮曘 |
{return} | integer | 杩斿洖宸茬粡鍒犻櫎鐨勯」鐩殑绱㈠紩銆 |
public function remove($item)
{
if(($index=$this->indexOf($item))>=0)
{
$this->removeAt($index);
return $index;
}
else
return false;
}
浠庡垪琛ㄤ腑鍒犻櫎涓涓」鐩 杩欎釜鍒楄〃棣栧厛浼氭悳绱㈣繖涓」鐩 鍦ㄥ垪琛ㄤ腑绗竴涓壘鍒扮殑椤圭洰浼氳鍒犻櫎銆(涔熷氨鏄, 鍒楄〃涓彲鑳藉瓨鍦ㄧ潃鐩稿悓鐨勯」鐩)
public mixed removeAt(integer $index)
| ||
$index | integer | 瑕佸垹闄ょ殑椤圭洰鐨勭储寮曘 |
{return} | mixed | 杩斿洖宸茬粡鍒犻櫎鐨勯」鐩 |
public function removeAt($index)
{
if(!$this->_r)
{
if($index>=0 && $index<$this->_c)
{
$this->_c--;
if($index===$this->_c)
return array_pop($this->_d);
else
{
$item=$this->_d[$index];
array_splice($this->_d,$index,1);
return $item;
}
}
else
throw new CException(Yii::t('yii','List index "{index}" is out of bound.',
array('{index}'=>$index)));
}
else
throw new CException(Yii::t('yii','The list is read only.'));
}
鍒犻櫎鎸囧畾浣嶇疆鐨勯」鐩
protected void setReadOnly(boolean $value)
| ||
$value | boolean | 璁剧疆杩欎釜鍒楄〃鏄惁涓哄彧璇 |
protected function setReadOnly($value)
{
$this->_r=$value;
}
public array toArray()
| ||
{return} | array | 鏁扮粍涓殑椤圭洰鍒楄〃 |