Computer >> 컴퓨터 >  >> 프로그램 작성 >> PHP

PHP ArrayAccess 인터페이스

<시간/>

소개

PHP에서 ArrayAccess 인터페이스는 배열인 속성 중 하나에 대한 액세스와 같은 배열을 제공하는 클래스를 개발하는 데 사용됩니다. 이러한 배열 속성은 개체 생성 중에 노출되지 않고 조작될 수 있습니다. 배열 액세스 인터페이스는 다음과 같은 추상 메소드를 정의합니다.

구문

ArrayAccess {
   /* Methods */
   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 - 오프셋 존재 여부

ArrayAccess::offsetGet − 검색할 오프셋

ArrayAccess::offsetSet − 지정된 오프셋에 값 할당

ArrayAccess::offsetUnset − 오프셋 설정을 해제합니다.

ArrayAccess 예

다음 예에서 연관 배열은 myclass의 내부 개인 자산입니다. 키는 오프셋 역할을 합니다. 배열의 항목을 설정, 검색 및 설정 해제할 수 있습니다. 오프셋을 지정하지 않으면 정수로 처리되어 다음 인덱스로 증가할 때마다

예시

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

)

클래스의 배열 속성은 인덱스 배열일 수도 있습니다. 이 경우 요소의 인덱스(0부터 시작)가 오프셋 역할을 합니다. offset 인수 없이 offsetSet(0 메소드를 호출할 때 배열의 인덱스는 다음 사용 가능한 정수로 증가합니다.

예시

<?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) {
      eturn 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
   )

)