return new Listing($value);
default:
throw new \InvalidArgumentException(sprintf('Type "%s" is not valid', $type));
}
},
$data
);
}
}
Match.php
<?php
include './model/Listing.php';
include 'ProListMatcher.php';
if($argc != 3){
die("Usage: Match.php <Products> <Listing>");
}
array_shift($argv);
/**
* @param string $data
* @return array
*/
function jsonToArray($value){
$ret = explode("\n", trim($value));
return array_map(function($value){
return json_decode($value, true);
}, $ret);
}
$matcher = new ProListMatcher(
jsonToArray(file_get_contents($argv[0])),
jsonToArray(file_get_contents($argv[1])));
$matches = $matcher->getMatches();
$matches = array_map(function($value){
return json_encode(array(
'product_name' => $value['product']->getProductName(),
'listing' => array_map(function(Listing $value){
return $value->convertToArray();
}, $value['listing'])
)
);
}, $matches
);
$output = fopen("output.txt", "w+");
file_put_contents($output, implode("\n", $matches));
fclose($output);
?>
这是我的代码,然后我在terminal运行:
chrisfus-MacBook-Pro:test chrisfu$ php Match.php products.txt listings.txt
PHP Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53
Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53
为什么会出现这样的错误,我没有两个叫Listing的 class,请大家帮忙看看,程序都贴上去了,大家可以运行试试。
------解决方案--------------------Match.php 中有
include './model/Listing.php';
include 'ProListMatcher.php';
ProListMatcher.php 中有
include './model/Product.php';
include './model/Listing.php';
include './model/BaseItem.php';
class Listing 不是被重复定义了吗?