php 미리 정 의 된 인터페이스
7482 단어 PHP
Traversable
Traversable {
}
역할:foreach 로 옮 겨 다 니 는 인 터 페 이 스 를 사용 할 수 있 는 지 확인 합 니 다.
php 코드 에 서 는 사용 할 수 없습니다.내부 의 PHP 클래스(C 로 쓰 는 클래스)만 Traversable 인터페이스 phop 코드 에서 Iterator 나 Iterator Aggregate 인 터 페 이 스 를 사용 하여 옮 겨 다 닐 수 있 습 니 다.
이 인 터 페 이 스 는 아무런 방법 이 없다.그 역할 은 모든 종류의 기본 인터페이스 일 뿐이다.
# foreach , Iterator
class man{
}
$p = new man();
var_dump($p instanceof man); #false
if( !is_array( $items ) && !$items instanceof Traversable )
//Throw exception here
Iterator 가 이 인 터 페 이 스 를 실현 하 는 대상 은 자신의 내부 데 이 터 를 교체 할 수 있 습 니 다.
Iterator extends Traversable
{
//
abstract public mixed current(void)
//
abstract public scalar key(void)
//
abstract public void next(void)
//
abstract public void rewind(void)
// , rewind() next()
abstract public boolean valid(void)
}
class myIterator implements Iterator {
private $position = 0;
private $array = array(
"firstelement",
"secondelement",
"lastelement",
);
public function __construct() {
$this->position = 0;
}
function rewind() {
// var_dump(__METHOD__);
$this->position = 0;
}
function current() {
// var_dump(__METHOD__);
return $this->array[$this->position];
}
function key() {
// var_dump(__METHOD__);
return $this->position;
}
function next() {
// var_dump(__METHOD__);
++$this->position;
}
function valid() {
// var_dump(__METHOD__);
return isset($this->array[$this->position]);
}
}
$it = new myIterator;
foreach($it as $key => $value) {
echo "{$key}|{$value}";
echo "
";
}
/**
0|firstelement
1|secondelement
2|lastelement
*/
http://www.nowamagic.net/librarys/veda/detail/2167
http://www.php.net/manual/zh/reserved.interfaces.php
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
laravel에 yo에서 angularJs&coffeescript를 사용할 수 있도록 한다.먼저 yo 명령을 사용할 수 있어야하므로 아래에서 설치 global에 설치한 곳에서 laravel의 프로젝트 루트로 이동. 클라이언트 코드를 관리하는 디렉토리를 만들고 이동합니다. 클라이언트 환경 만들기 이것으로 히...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.