บทนำ
ใน PHP Interface ArrayAccess ใช้สำหรับพัฒนาคลาสที่ให้การเข้าถึงแบบอาร์เรย์ (array-like access) ให้กับ property หนึ่งที่เป็นอาร์เรย์ Property อาร์เรย์ดังกล่าวสามารถจัดการได้โดยไม่ต้อง expose มันระหว่างการสร้าง object Interface ArrayAccess กำหนด abstract methods ดังนี้
ไวยากรณ์
ArrayAccess { abstract public offsetExists ( mixed $offset ) : bool abstract public offsetGet ( mixed $offset ) : mixed abstract public offsetSet ( mixed $offset , mixed $value ) : void abstract public offsetUnset ( mixed $offset ) : void }เมธอด
ArrayAccess::offsetExists - ตรวจสอบว่ามี offset นั้นหรือไม่
ArrayAccess::offsetGet - ดึงค่าจาก offset
ArrayAccess::offsetSet - กำหนดค่าให้ offset ที่ระบุ
ArrayAccess::offsetUnset - ลบ offset
ArrayAccess Example
ในตัวอย่างต่อไปนี้ associative array เป็น internal private property ของ myclass คีย์ทำหน้าที่เป็น offset สามารถ set, retrieve และ unset item ในอาร์เรย์ได้ หากไม่ให้ offset จะถูกนับเป็น integer เพิ่มขึ้นทีละ 1 ทุกครั้ง
ตัวอย่าง
<?php class myclass implements ArrayAccess { private $arr = array(); public function __construct() { $this->arr = array( "Mumbai" => "Maharashtra", "Hyderabad" => "A.P.", "Patna" => "Bihar" ); } public function offsetSet($offset, $value) { if (is_null($offset)) { $this->arr[] = $value; } else { $this->arr[$offset] = $value; } } public function offsetExists($offset) { return isset($this->arr[$offset]); } public function offsetUnset($offset) { unset($this->arr[$offset]); } public function offsetGet($offset) { return isset($this->arr[$offset]) ? $this->arr[$offset] : null; } } $obj = new myclass(); var_dump(isset($obj["Mumbai"])); var_dump($obj["Mumbai"]); unset($obj["Mumbai"]); var_dump(isset($obj["Mumbai"])); $obj["Bombay"] = "Maharashtra"; var_dump($obj["Bombay"]); $obj["Chennai"] = 'Tamilnadu'; $obj[] = 'New State'; $obj["Hyderabad"] = 'Telangana'; print_r($obj); ?>ผลลัพธ์
โปรแกรมข้างต้นแสดงผลลัพธ์ดังนี้
bool(true) string(11) "Maharashtra" bool(false) string(11) "Maharashtra" myclass Object( [arr:myclass:private] => Array( [Hyderabad] => Telangana [Patna] => Bihar [Bombay] => Maharashtra [Chennai] => Tamilnadu [0] => New State ) )
Property อาร์เรย์ของคลาสก็สามารถเป็น indexed array ได้ ในกรณีนี้ index ของ element (เริ่มจาก 0) ทำหน้าที่เป็น offset เมื่อเรียก offsetSet() โดยไม่มี offset argument index ของอาร์เรย์จะเพิ่มขึ้นเป็น integer ถัดไปที่ว่าง
ตัวอย่าง
<?php class myclass implements ArrayAccess { private $arr = array(); public function __construct() { $this->arr = array("Mumbai", "Hyderabad", "Patna"); } public function offsetSet($offset, $value) { if (is_null($offset)) { $this->arr[] = $value; } else { $this->arr[$offset] = $value; } } public function offsetExists($offset) { return isset($this->arr[$offset]); } public function offsetUnset($offset) { unset($this->arr[$offset]); } public function offsetGet($offset) { return isset($this->arr[$offset]) ? $this->arr[$offset] : null; } } $obj = new myclass(); var_dump(isset($obj[0])); var_dump($obj[0]); unset($obj[0]); var_dump(isset($obj[0])); $obj[3] = "Pune"; var_dump($obj[3]); $obj[4] = 'Chennai'; $obj[] = 'NewDelhi'; $obj[2] = 'Benguluru'; print_r($obj); ?>ผลลัพธ์
โปรแกรมข้างต้นแสดงผลลัพธ์ดังนี้
bool(true) string(6) "Mumbai" bool(false) string(4) "Pune" myclass Object( [arr:myclass:private] => Array( [1] => Hyderabad [2] => Benguluru [3] => Pune [4] => Chennai [5] => NewDelhi ) )