时间:2021-07-01 10:21:17 帮助过:3人阅读
buildFromArray($fields); } /** * @param array $fields */ abstract public function buildFromArray(array $fields); /** * @return array */ abstract public function convertToArray();}?>
$this->title, 'manufacturer' => $this->manufacturer, 'currency' => $this->currency, 'price' => $this->price, ); } /** * @inheritDoc */ public function buildFromArray(array $fields) { $this->title = $fields['title']; $this->manufacturer = $fields['manufacturer']; $this->currency = $fields['currency']; $this->price = $fields['price']; } public function getCurrency() { return $this->currency; } public function getManufacturer() { return $this->manufacturer; } public function getPrice() { return $this->price; } public function getTitle() { return $this->title; }}
productName = $fields['product_name']; $this->manufacturer = $fields['manufacturer']; $this->family = isset($fields['family']) ? $fields['family'] : null; $this->model = $fields['model']; $this->announcedDate = $fields['announced-date']; } /** * @inheritDoc */ public function convertToArray() { return array( 'product_name' => $this->productName, 'manufacturer' => $this->manufacturer, 'family' => $this->family, 'model' => $this->model, 'announced-date' => $this->announcedDate, ); } public function getAnnouncedDate() { return $this->announcedDate; } public function getFamily() { return $this->family; } public function getManufacturer() { return $this->manufacturer; } public function getModel() { return $this->model; } public function getProductName() { return $this->productName; }}?>
products = $this->normalizeRawData($products, self::TYPE_PRODUCT); $this->listings = $this->normalizeRawData($listings, self::TYPE_LISTING); } /** * Matches up listings to products * * @return array */ public function getMatches() { $ret = array(); // loop through all products foreach ($this->products as $product) { // reset matching listings $matchingListings = array(); // loop through all listings foreach ($this->listings as $key => $listing) { $matches = 0; // match on manufacturer if (strtolower($product->getManufacturer()) == strtolower($listing->getManufacturer())) { $matches++; } // match on model in listing title $result = strpos(strtolower($listing->getTitle()), strtolower($product->getModel())); if (false !== $result) { $matches++; } if (2 === $matches) { $matchingListings[] = $listing; // has been matched, remove listing from future searches unset($this->listings[$key]); } } if (count($matchingListings)) { // 1 or more matches were found, add to output $ret[] = array( 'product' => $product, 'listings' => $matchingListings ); } } return $ret; } /** * Convert plain nested array to array of objects * * @param array $data * @param int $type * @return array * @throws \InvalidArgumentException */ protected function normalizeRawData(array $data, $type) { // build model for each item return array_map(function($value) use ($type) { switch ($type) { case ProListMatcher::TYPE_PRODUCT: return new Product($value); case ProListMatcher::TYPE_LISTING: return new Listing($value); default: throw new \InvalidArgumentException(sprintf('Type "%s" is not valid', $type)); } }, $data ); }}
");}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);?>
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 53Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53
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 不是被重复定义了吗?
Match.php 中有
include './model/Listing.php';
include 'ProListMatcher.php';
ProListMatcher.php 中有
include './model/Product.php';
include './model/Listing.php';
include './model/Base……
原来是这样的,那如果我在Match.php需要用到那两个文件,是用use吗?
那你在 Product.php、Listing.php 就怎么会用 include _once 'BaseItem.php';
once 一次
include_once 已加载了就不加载
那你在 Product.php、Listing.php 就怎么会用 include_once 'BaseItem.php';
once 一次
include_once 已加载了就不加载
对的,以前没太注意include和include_once的区别,学习了
LS也遇到过相同问题,这是我的解决方法: http://www.ibihuo.com/show-56.html