>  기사  >  백엔드 개발  >  PHP ArrayAccess 인터페이스

PHP ArrayAccess 인터페이스

王林
王林앞으로
2023-09-05 23:01:021438검색

PHP ArrayAccess 接口

소개

PHP에서 ArrayAccess 인터페이스는 배열 속성 중 하나에 배열과 유사한 액세스를 제공하는 클래스를 개발하는 데 사용됩니다. 이러한 배열 속성은 노출하지 않고도 객체 생성 중에 조작될 수 있습니다. 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: :오프셋세트 - 지정된 오프셋에 값을 할당합니다.

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

ArrayAccess::offsetUnset - 오프셋 설정 해제. p>

ArrayAccess 예제

아래 예제에서 연관 배열은 myclass의 내부 전용 속성입니다. 키는 오프셋 역할을 합니다. 배열의 항목을 설정, 검색 및 설정 해제할 수 있습니다. 오프셋이 제공되지 않으면 정수로 처리되어 매번 다음 인덱스로 증가됩니다.

Example

라이브 데모

<?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"] = &#39;Tamilnadu&#39;;
$obj[] = &#39;New State&#39;;
$obj["Hyderabad"] = &#39;Telangana&#39;;
print_r($obj);
?>

Output

위 프로그램은 다음 출력을 보여줍니다.

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부터 시작)가 오프셋 역할을 합니다. offsetSet(0 메소드가 오프셋 매개변수 없이 호출되면 배열 인덱스는 사용 가능한 다음 정수로 증가됩니다.

Example

<?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] = &#39;Chennai&#39;;
$obj[] = &#39;NewDelhi&#39;;
$obj[2] = &#39;Benguluru&#39;;
print_r($obj);
?>

Output

위 프로그램은 다음 출력을 보여줍니다

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
   )

)

위 내용은 PHP ArrayAccess 인터페이스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제