类型提示,因此属性是特定对象类型的数组

时间:2019-06-26 02:22:29

标签: php type-hinting

我正在尝试在PHP 7.3中构建名为AcmeCorpConfiguration的配置类,最终将使用JSON文件中的数据填充该配置类。但是,在此行public Item $itemList = [];上发生了解析错误-它表示解析错误:C:\ xampp中的语法错误,语法异常,意外的'Item'(T_STRING),期望的函数(T_FUNCTION)或const(T_CONST)第217行的\ htdocs \ PopulateObjectsFromJSONFile \ version4.php 。这是模型类:

class Item
{   
    public $itemID;
    public $description;
}

class Location
{    
    public $storeID;
    public $city;
    public $state;
}

class Fleet
{    
    public $vehicleID;
    public $type;
    public $model;
    public $year;
}

class AcmeCorpConfiguration
{
    public $company;
    public Item $itemList = [];
    public Location $locationList = [];
    public Fleet $fleetList = [];
}

这是JSON文件的内容:

{
    "Company":"Acme Corporation",
    "Items": [
        {"itemID":72, "description":"drill"},
        {"itemID":73, "description":"hammer"},
        {"itemID":74, "description":"pliers"}
    ],
    "Locations": [
        {"storeID":7, "city":"Omaha", "state":"NE"},
        {"storeID":9, "city":"Madison", "state":"WI"}
    ],
    "Fleet": [
        {"vehicleID":92, "type":"truck", "model":"Ford F-150", "year":2015},
        {"vehicleID":93, "type":"truck", "model":"GMC Sierra 1500", "year":2018},
        {"vehicleID":94, "type":"truck", "model":"Toyota Tundra", "year":2018},
        {"vehicleID":95, "type":"car", "model":"Toyota Lexus", "year":2019}
    ]
}

为什么会给出解析错误?我在C#中做了类似的事情,但是类型提示语法或通用方法可能不适用于PHP。

0 个答案:

没有答案