PHP ArrayAccess 인터페이스

王林
풀어 주다: 2023-09-06 06:00:01
앞으로
1452명이 탐색했습니다.

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 - 검색할 오프셋 < /p>

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으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!