class 生成器的对象无法转换为字符串
Object of class Generator could not be converted to string
我有一些代码可以 return 海量数据。因此,我不想将其保存到数组中,而是想使用生成器。但是,我遇到了很多问题。
现在,当我像这样简单地做的时候
foreach ($this->test(0,10) as $test) {
print $test;
}
public function test($from, $to) {
for ($i = $from; $i < $to; $i++) {
yield $i;
}
}
这里我得到以下输出
0123456789
现在,我想用一个现实生活中的例子来做同样的事情。像这样
$posts = $this->getPosts();
foreach ($posts as $post) {
print $post;
}
protected function getPosts()
{
// Getting the content of the sitemap
$response = Curl::get('http://wpde.org/sitemap.xml')[0];
// Loading the string as XML
$sitemapJson = simplexml_load_string($response->getContent());
// This case is true when the sitemap has a link to more sitemaps instead of linking directly to the posts
if (isset($sitemapJson->sitemap)) {
foreach ($sitemapJson->sitemap as $post) {
if (substr($post->loc, -3) === "xml") {
$this->setUrl((string)$post->loc);
yield $this->getPosts(); // I also tried it here without the yield, but then I just get an empty output :/
}
}
}
// This case is true, when the sitemap has now the direct links to the post
elseif (isset($sitemapJson->url)) {
foreach ($sitemapJson->url as $url) {
yield (string)$url->loc;
}
}
}
但是这里出现错误:
Catchable fatal error: Object of class Generator could not be converted to string
奇怪的是,当我打印出 URL
print $this->url . '<br>';
我只获得站点地图的第一个 URL。但是,如果我删除 yield
部分,我将获得站点地图的所有链接。好像不知何故就停在那里了?
无论如何,我只是无法打印出数据。我该怎么做?谢谢!
如果第一个 if 情况下没有 yield 就根本没有内容。
更改此行
yield $this->getPosts();
至
foreach($this->getPosts() as $post)
yield $post;
因为只有一个项目,不是全部"collection"可能会一次产生。
我有一些代码可以 return 海量数据。因此,我不想将其保存到数组中,而是想使用生成器。但是,我遇到了很多问题。
现在,当我像这样简单地做的时候
foreach ($this->test(0,10) as $test) {
print $test;
}
public function test($from, $to) {
for ($i = $from; $i < $to; $i++) {
yield $i;
}
}
这里我得到以下输出
0123456789
现在,我想用一个现实生活中的例子来做同样的事情。像这样
$posts = $this->getPosts();
foreach ($posts as $post) {
print $post;
}
protected function getPosts()
{
// Getting the content of the sitemap
$response = Curl::get('http://wpde.org/sitemap.xml')[0];
// Loading the string as XML
$sitemapJson = simplexml_load_string($response->getContent());
// This case is true when the sitemap has a link to more sitemaps instead of linking directly to the posts
if (isset($sitemapJson->sitemap)) {
foreach ($sitemapJson->sitemap as $post) {
if (substr($post->loc, -3) === "xml") {
$this->setUrl((string)$post->loc);
yield $this->getPosts(); // I also tried it here without the yield, but then I just get an empty output :/
}
}
}
// This case is true, when the sitemap has now the direct links to the post
elseif (isset($sitemapJson->url)) {
foreach ($sitemapJson->url as $url) {
yield (string)$url->loc;
}
}
}
但是这里出现错误:
Catchable fatal error: Object of class Generator could not be converted to string
奇怪的是,当我打印出 URL
print $this->url . '<br>';
我只获得站点地图的第一个 URL。但是,如果我删除 yield
部分,我将获得站点地图的所有链接。好像不知何故就停在那里了?
无论如何,我只是无法打印出数据。我该怎么做?谢谢!
如果第一个 if 情况下没有 yield 就根本没有内容。
更改此行
yield $this->getPosts();
至
foreach($this->getPosts() as $post)
yield $post;
因为只有一个项目,不是全部"collection"可能会一次产生。