Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

PHP ArrayAccess interface

Giới thiệu

Trong PHP, interface ArrayAccess dùng để phát triển class cung cấp truy cập kiểu mảng cho một thuộc tính là mảng. Thuộc tính mảng đó có thể thao tác mà không cần lộ ra khi tạo đối tượng. ArrayAccess định nghĩa các phương thức trừu tượng:

Cú pháp

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 }

Phương thức

  • ArrayAccess::offsetExists - Kiểm tra offset tồn tại.
  • ArrayAccess::offsetGet - Lấy giá trị tại offset.
  • ArrayAccess::offsetSet - Gán giá trị cho offset.
  • ArrayAccess::offsetUnset - Xóa offset.

Ví dụ ArrayAccess

Mảng liên kết là thuộc tính private của myclass. Khóa đóng vai offset. Có thể set, lấy, unset mục trong mảng. Nếu không cho offset, coi như số nguyên, tăng dần chỉ mục tiếp theo.

Ví dụ

<?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); ?>

Kết quả

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 ) )

Thuộc tính mảng cũng có thể là indexed array. Lúc đó chỉ mục (bắt đầu 0) làm offset. Khi gọi offsetSet() không có offset, chỉ mục tăng lên số nguyên tiếp theo.

Ví dụ

<?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); ?>

Kết quả

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 ) )