Rumah > pembangunan bahagian belakang > tutorial php > 请大家帮忙看看问题

请大家帮忙看看问题

WBOY
Lepaskan: 2016-06-23 14:19:24
asal
1131 orang telah melayarinya

程序是要输入两个json格式的txt文件,然后通过匹配两个文件的相同的index,然后输出一个新的json格式的txt文件。 比如:
这是输入的两个文件的格式:
1. Product
{
  "product_name": String   // A unique id for the product
  "manufacturer": String
  "family": String         // optional grouping of products
  "model": String
  "announced-date": String // ISO-8601 formatted date string, e.g. 2011-04-28T19:00:00.000-05:00
}
2. Listing
{
  "title": String         // description of product for sale
  "manufacturer":  String // who manufactures the product for sale
  "currency": String      // currency code, e.g. USD, CAD, GBP, etc.
  "price": String         // price, e.g. 19.99, 100.00
}

输出的文件格式为:
{
  "product_name": String
  "listings": Array[Listing]
}

先上代码:
BaseItem.php

<?phpabstract class BaseItem{    /**     * @param array $fields     */    public function __construct(array $fields)    {        $this->buildFromArray($fields);    }    /**     * @param array $fields     */    abstract public function buildFromArray(array $fields);    /**     * @return array     */    abstract public function convertToArray();}?>
Salin selepas log masuk


Listing.php
<?phpinclude_once 'BaseItem.php';class Listing extends BaseItem{    protected $title;    protected $manufacturer;    protected $currency;    protected $price;    /**     * @inheritDoc     */    public function convertToArray()    {        return array(            'title' => $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;    }}
Salin selepas log masuk


Product.php
<?phpinclude_once 'BaseItem.php';class Product extends BaseItem{    protected $productName;    protected $manufacturer;    protected $family;    protected $model;    protected $announcedDate;    /**     * @inheritDoc     */    public function buildFromArray(array $fields)    {        $this->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;    }}?>
Salin selepas log masuk


ProListMatcher.php
<?phpinclude './model/Product.php';include './model/Listing.php';include './model/BaseItem.php';class ProListMatcher{    const TYPE_PRODUCT = 1;    const TYPE_LISTING = 2;    /** @var Product[] */    protected $products;    /** @var Listing[] */    protected $listings;    /**     * @param array $products     * @param array $listings     */    public function __construct(array $products, array $listings)    {        $this->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        );    }}
Salin selepas log masuk


Match.php
<?phpinclude './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);?>
Salin selepas log masuk


这是我的代码,然后我在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 53Fatal error: Cannot redeclare class Listing in /Users/chrisfu/Sites/test/model/Listing.php on line 53
Salin selepas log masuk

为什么会出现这样的错误,我没有两个叫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 不是被重复定义了吗?

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan