在下面的代码中,我的输出中没有“处理”。我看到文件句柄是一个资源,文件被打开,调用FqReader的构造函数,我检查了所有这些。但是执行FqReader :: getread()时,我看不到输出,返回的数组为空。当我把while(1)而不是现在的代码中的逻辑测试放在第一个while循环时也没有得到解释。
<?php
class FastqFile {
function __construct($filename) {
if (substr($filename, -3, 3) == '.gz') {
$this->handle = gzopen($filename, 'r');
return $this->handle;
}
else
$this->handle = fopen($filename, 'r');
return $this->handle;
}
}
class FqReader {
function __construct($file_handle) {
$this->handle = $file_handle;
}
function getread() {
while ($header = fgets($this->handle) !== false) {
echo "handled";
$bases = fgets($this->handle);
$plus = fgets($this->handle);
$scores = fgets($this->handle);
yield array($header, $plus, $scores);
}
}
}
$filename = $argv[1];
$file_handle = new FastqFile($filename);
var_dump($file_handle);
$reader = new FqReader($file_handle);
var_dump($reader->getread());
输出:
object(FastqFile)#1 (1) {
["handle"]=>
resource(5) of type (stream)
}
object(Generator)#3 (0) {
}
答案 0 :(得分:2)
class Foo {
public:
Foo (int pInt) {
....
}
Foo (double pDouble) {
....
}
int i = 5;
Foo foo(i); // explicit constructors
Foo foo2(27);
Foo foo3(2.9);
Foo foo4 = i; // implicit constructors
Foo foo5 = 27;
Foo foo6 = 2.1;
是$file_handle
个实例。然后将该对象传递给FastqFile
,但是您需要将该对象的句柄传递给fgets()
。例如:
fgets()
使用class FqReader {
function __construct($file_handle) {
$this->handle = $file_handle->handle;
}
function getread() {
while ($header = fgets($this->handle) !== false) {
echo "handled";
$bases = fgets($this->handle);
$plus = fgets($this->handle);
$scores = fgets($this->handle);
yield array($header, $plus, $scores);
}
}
}
并没有向您显示该错误。
答案 1 :(得分:0)
确切地说,这就像一个魅力:
(使用函数打开文件,而不是类)
function openfq($filename)
{
if (substr($filename, -3, 3) == '.gz') {
$handle = gzopen($filename, 'r');
return $handle;
}
else
$handle = fopen($filename, 'r');
return $handle;
}
class FqReader {
function __construct($file_handle) {
$this->handle = $file_handle;
}
function getread() {
while (($header = fgets($this->handle)) !== false) {
echo "handled";
$bases = fgets($this->handle);
$plus = fgets($this->handle);
$scores = fgets($this->handle);
yield array($header, $bases, $scores);
}
}
}
$filename = $argv[1];
$file_handle = openfq($filename);
var_dump($file_handle);
$reader = new FqReader($file_handle);
var_dump($reader->getread());
foreach($reader->getread() as $read) {
var_dump($read);
}