【发布时间】:2011-12-24 17:46:19
【问题描述】:
我对编程非常陌生,并且已经将我的头撞到墙上试图制作这个网络抓取工具 2 天了。我简化了完整的脚本(甚至删除了所有实际的网络抓取),同时保持了原来的功能障碍。
我想代码对于受过训练的眼睛来说很容易理解,但为了方便起见,我会说脚本应该:
- 用子数组填充数组
- 为每个子数组设置一些值,但将最后一个子数组值留空
- a) 使用另一个函数 3.b) 获取最后一个子数组值,然后将它们插入到原始数组中
3.b 是脚本失败的地方。不输入值(留空)。
我知道我正在使用没有参数的函数(完整的代码包括它们),这可能很糟糕,但没有它们,功能障碍仍然存在。
<?php
$scrape = new Scraper();
class Scraper
{
protected $cars = array();
function __construct()
{
$this->getcars();
foreach ($this->cars as $item) {
$item['color'] = $this->getcolor($item); // here is the fault!
}
}
private function getcars()
{
$listofcars = array('0','1','2');
foreach ($listofcars as $item) {
$this->cars[] = array('carname' => 'humvee','color' => '');
}
}
private function getcolor()
{
return 'green';
}
}
?>
【问题讨论】:
-
$this->getcolor() WITH参数的定义在哪里?
-
这个问题我恐怕不明白。不过,我将提供整个代码的链接:link 根据 GigaWatt 或 Grok 建议的更改,它终于可以工作了 - 非常感谢!
-
在 GigaWatt 答案的编辑中有解释。
标签: php class function constructor web-scraping