IteratorAggregate en SeekableIterator tegelijk implementeren
Ik probeer in een van classes zowel de SeekableIterator als de IteratorAggregate te implementeren. Alleen, dan krijg ik de volgende fout:
Fatal error: Class E\Core\Database\DataResult cannot implement both Iterator and IteratorAggregate at the same time in /home/user/public_html/lib/ECMS/Core/Database/DataResult.php on line 11
Ik zie wel in dat beide interfaces de Iterator class implementeren, maar dit kan toch de bedoeling niet zijn?
Weet iemand hier een oplossing voor?
Gewijzigd op 04/08/2012 15:58:05 door Write Down
Bump :-)
Ik denk dat je wil dat de iterator die de getIterator() functie aanbiedt, SeekableIterator implementeert, dat kan toch prima?
Hoe zou ik dat dan moeten doen? Je kan toch interface implementeren bij een bepaalde functie?
Laat je code eens zien.
Gewijzigd op 14/08/2012 18:14:18 door Write Down
De volgende klasse wordt gebruikt vanuit de database klasse. Deze geeft altijd een Result (of een exception) terug.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
De eerste uitbreiding, bedoeld bijvoorbeeld een insert statement ziet er als volgt uit:
Code (php)
De klasse waarmee ik dan select statements e.t.c. wil verwerken ziet er als volgt uit:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace E\Core\Database;
use E\Core\Database\Result;
use E\Core\Database\SeekableResult;
use E\Core\Database\SeekableIteratorInterface;
class DataResult extends Result implements \IteratorAggregate, \Countable, SeekableIteratorInterface {
private $seekableResult;
public function __construct(Result $result) {
$this->setResult($result->getResult()->fetchAll(\PDO::FETCH_ASSOC));
$this->setSeekableResult($this->getResult());
}
private function setSeekableResult(array $result) {
$this->seekableResult = new SeekableResult($result);
}
private function getSeekableResult() {
return $this->seekableResult;
}
public function getIterator() {
$result = new SeekableResult($this->getResult());
return $result->getItterator();
}
public function count() {
return count($this->getResult());
}
////-->SeekableIteratorInterface
public function current() {
return $this->getSeekableResult()->current();
}
public function key() {
return $this->getSeekableResult()->key();
}
public function next() {
$this->getSeekableResult()->next();
}
public function rewind() {
$this->getSeekableResult()->rewind();
}
public function seek($position) {
return $this->getSeekableResult()->seek($position);
}
public function valid() {
$this->getSeekableResult()->valid();
}
////
}
?>
namespace E\Core\Database;
use E\Core\Database\Result;
use E\Core\Database\SeekableResult;
use E\Core\Database\SeekableIteratorInterface;
class DataResult extends Result implements \IteratorAggregate, \Countable, SeekableIteratorInterface {
private $seekableResult;
public function __construct(Result $result) {
$this->setResult($result->getResult()->fetchAll(\PDO::FETCH_ASSOC));
$this->setSeekableResult($this->getResult());
}
private function setSeekableResult(array $result) {
$this->seekableResult = new SeekableResult($result);
}
private function getSeekableResult() {
return $this->seekableResult;
}
public function getIterator() {
$result = new SeekableResult($this->getResult());
return $result->getItterator();
}
public function count() {
return count($this->getResult());
}
////-->SeekableIteratorInterface
public function current() {
return $this->getSeekableResult()->current();
}
public function key() {
return $this->getSeekableResult()->key();
}
public function next() {
$this->getSeekableResult()->next();
}
public function rewind() {
$this->getSeekableResult()->rewind();
}
public function seek($position) {
return $this->getSeekableResult()->seek($position);
}
public function valid() {
$this->getSeekableResult()->valid();
}
////
}
?>
De interface die ik in de bovenstaande klasse implementeer, SeekableIteratorInterface, is eigenlijk dezelfde als de SeekableIterator van PHP. (Alleen, ik moest dit zo doen, net door het probleem dat ik mijn eerste bericht aankaartte.)
Code (php)
En dan uiteindelijk de klasse die het mogelijk maakt om 'Seekable' te gebruiken:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace E\Core\Database;
class SeekableResult implements \SeekableIterator{
private $result;
private $position;
public function __construct(array $result) {
$this->setResult($result);
$this->setPosition(0);
}
private function setResult($result) {
$this->result = $result;
}
public function getResult() {
return $this->result;
}
private function setPosition($position) {
$this->position = $position;
}
private function getPosition() {
return $this->position;
}
public function current() {
$result = $this->getResult();
$current = $result[$this->getPosition()];
if(is_array($current)) {
$current = (object) $current;
}
return $current;
}
public function key() {
return $this->getPosition();
}
public function next() {
$position = $this->getPosition();
$this->setPosition($position++);
}
public function rewind() {
$this->setPosition(0);
}
public function seek($position) {
$this->setPosition($position);
if(!is_int($this->getPosition())) {
throw new \InvalidArgumentException('Seek position must be an integer.');
}
if (!$this->valid()) {
throw new OutOfBoundsException('Invalid seek position.');
}
}
public function valid() {
$result = $this->getResult();
return isset($result[$this->getPosition()]);
}
public function getItterator() {
return new \ArrayIterator($this->result);
}
}
?>
namespace E\Core\Database;
class SeekableResult implements \SeekableIterator{
private $result;
private $position;
public function __construct(array $result) {
$this->setResult($result);
$this->setPosition(0);
}
private function setResult($result) {
$this->result = $result;
}
public function getResult() {
return $this->result;
}
private function setPosition($position) {
$this->position = $position;
}
private function getPosition() {
return $this->position;
}
public function current() {
$result = $this->getResult();
$current = $result[$this->getPosition()];
if(is_array($current)) {
$current = (object) $current;
}
return $current;
}
public function key() {
return $this->getPosition();
}
public function next() {
$position = $this->getPosition();
$this->setPosition($position++);
}
public function rewind() {
$this->setPosition(0);
}
public function seek($position) {
$this->setPosition($position);
if(!is_int($this->getPosition())) {
throw new \InvalidArgumentException('Seek position must be an integer.');
}
if (!$this->valid()) {
throw new OutOfBoundsException('Invalid seek position.');
}
}
public function valid() {
$result = $this->getResult();
return isset($result[$this->getPosition()]);
}
public function getItterator() {
return new \ArrayIterator($this->result);
}
}
?>
Graag jullie meningen!
Ik heb ontdekt dat ArrayIterator de SeekableInterface implementeert. M.a.w. ik kan die gewoon lekker gebruiken...
Reguliere extension werkt toch prima?
Dit heb ik toch ook niet Pim? Maar goed, ik denk dat jij eerder doelt waarom ik ManipulationResult niet extend naar DataResult?