php一開始是面向過程的語言,到后期才支持面向對象,數組在php中的類型是 “array”:
echo gettype(array());輸出
array很多操作數組的函數都是以 “array” 開頭,第一個參數為要操作的數組。
要實現一個數組類,需要實現Arrayaccess這個接口,這個接口的功能是 “提供像訪問數組一樣訪問對象的能力” ,該接口有四個方法:
abstract public boolean offsetExists ( mixed $offset )abstract public mixed offsetGet ( mixed $offset )abstract public void offsetSet ( mixed $offset , mixed $value )abstract public void offsetUnset ( mixed $offset )這四個函數的作用如下(假設$obj是一個實現了該接口的類的實例):
offsetExists, 執行isset( $obj[$key])時觸發offsetGe,獲取$obj[$key]時觸發offsetSet, 執行 $obj[$key] = $value時觸發offsetUnset, 執行unset($obj[$key])時觸發看上去有點類似C++的運算符重載。我們可以封裝一個類,以一個數組變量作為其私有屬性,這四個函數操作數組變量就行。
且看代碼: 我們把這個類命名為XArray。
class XArray implements ArrayAccess{ PRivate $container = array(); public function __construct($size = 0, $value = 0) { if ($size > 0) { $this->container = array_fill(0, $size, $value); } } public function offsetSet($offset, $value) { echo "call ", __METHOD__, PHP_EOL; if(is_null($offset)) { $this->container[] = $value; } else { $this->container[$offset] = $value; } } public function offsetUnset($offset) { echo "call ", __METHOD__, PHP_EOL; unset($this->container[$offset]); } public function offsetGet($offset) { echo "call ", __METHOD__, PHP_EOL; return isset($this->container[$offset]) ? $this->container[$offset] : null; } public function offsetExists($offset) { echo "call ", __METHOD__, PHP_EOL; return isset($this->container[$offset]); }}測試:
$obj = new XArray(5, 10);$obj[] = 16;echo $obj['a'];isset($obj[3]);unset($obj['a']);輸出:
call XArray::offsetSetcall XArray::offsetGetcall XArray::offsetExistscall XArray::offsetUnset正如以上分析所言。
我們可以像操作數組一樣操作一個XArray類的實例,可是對于便利操作(foreach),就不行了。
foreach ($obj as $v) { echo $v;}沒有輸出。
要能讓XArray的實例實現遍歷操作,得實現Traversable接口,但這是個抽象接口,不過Iterator接口繼承了Traversable接口,所以我們可以實現Iterator接口。這個接口有5個方法:
abstract public mixed current ( void )abstract public scalar key ( void )abstract public void next ( void )abstract public void rewind ( void )abstract public boolean valid ( void )各方法的作用如下:
current, 返回當前元素key, 返回當前元素的鍵next, 移動到下一個元素rewind, 返回迭代器的第一個元素valid, 在rewind和next方法之后調用,檢查當前位置是否有效在XArray中增加一個$position私有變量,然后增加以下5個方法:
public function rewind() { echo "call ", __METHOD__, PHP_EOL; reset($this->container); $this->position = 0; } public function current() { echo "call ", __METHOD__, PHP_EOL; return current($this->container); } public function next() { echo "call ", __METHOD__, PHP_EOL; next($this->container); $this->position++; } public function key() { echo "call ", __METHOD__, PHP_EOL; return key($this->container); } public function valid() { echo "call ", __METHOD__, PHP_EOL; return $this->position < count($this->container); }有rewind, current,next,key這4個方法,內部都是通過調用php操作數組的相應方法來實現的。
測試:
$obj = new XArray();$obj[] = 1;$obj[] = 2;$obj[] = 3;foreach ($obj as $v) { echo $v, PHP_EOL;}輸出:
call XArray::offsetSetcall XArray::offsetSetcall XArray::offsetSetcall XArray::rewindcall XArray::validcall XArray::current1call XArray::nextcall XArray::validcall XArray::current2call XArray::nextcall XArray::validcall XArray::current3call XArray::nextcall XArray::valid由以上輸出可知,首次遍歷時,依次調用rewind, valid方法, 然后調用current方法得到值;后面都是依次調用next, valid方法,再調用current方法得到值。如果valid方法返回false,說明遍歷到了末尾,則不再調用current方法,遍歷結束。
可以看到,用 foreach($obj as $v)這種遍歷方式,并沒有調用key方法。
我們把遍歷方式改為foreach($obj as $k => $v)試一下:
$obj = new XArray();$obj[] = 1;$obj[] = 2;$obj[] = 3;foreach ($obj as $k => $v) { echo $v, PHP_EOL;}call XArray::offsetSetcall XArray::offsetSetcall XArray::offsetSetcall XArray::rewindcall XArray::validcall XArray::currentcall XArray::key1call XArray::nextcall XArray::validcall XArray::currentcall XArray::key2call XArray::nextcall XArray::validcall XArray::currentcall XArray::key3call XArray::nextcall XArray::valid這次有調用key方法了,而且可以看到, key方法是在current方法調用之后才調用的。
這樣的數組類,功能還是有點弱,很多數組的方法都沒有,比如,push, pop, slice, 也不能獲取數組長度(length屬性)。
在XArray類中添加如下代碼:
public function all() { return $this->container; } /** $obj->length 獲取數組元素個數 */ public function __get($property) { if ($property == 'length') { return count($this->container); } return null; } /** 把一個XArray類的實例,數組或其他類型的變量合并到本實例的數組變量中 */ public function merge($data) { $class = get_class($this); if ($data instanceof $class) { $this->container = array_merge($this->container, $data->all()); } elseif (is_array($data)) { $this->container = array_merge($this->container, $data); } else{ $this->container[] = $data; } return $this; } public function shift() { return array_shift($this->container); } public function pop() { return array_pop($this->container); } public function push($ele) { foreach (func_get_args() as $v) { array_push($this->container, $v); } return $this; } public function unshift($ele) { foreach (func_get_args() as $v) { array_unshift($this->container, $v); } return $this; } public function slice($offset, $length) { $arr = array_slice($this->container, $offset, $length); if (empty($arr)) { return null; } $class = get_class($this); $obj = new $class(); return $obj->merge($arr); } /** 打印實例內容 */ public function dump() { echo "[elements begin]", PHP_EOL; foreach ($this->container as $k => $v) { echo "/t",$k, " => ", $v, PHP_EOL; } echo "[elements end]", PHP_EOL; }大部分函數都是調用php的同名array_系列數組操作函數,其中unshift, push方法最后時return $this,可以實現鏈式調用, slice方法則是返回一個XArray對象。
測試代碼:
$obj = new XArray(2, 6);$obj[] = 16;$obj->push(1,2,3)->unshift(5)->pop();$obj->dump();echo "len(obj) = ", $obj->length, PHP_EOL;$obj2 = $obj->slice(0, 3);$obj2->dump();輸出:
call XArray::offsetSet[elements begin] 0 => 5 1 => 6 2 => 6 3 => 16 4 => 1 5 => 2[elements end]len(obj) = 6[elements begin] 0 => 5 1 => 6 2 => 6[elements end]如預期。
由于時間有限,數組操作的其他功能就不實現了。之前查看Laravel的源碼,有個文件也是實現了一個數組類,很多數組操作的方法都實現了。
新聞熱點
疑難解答
圖片精選