class Thing
{
public $tid;
//$tid 所对应的构造参数是 $id
public $name;
public $zone;
public $publish_time
public $smallimg_url;
public $isRented;
/**
* ThingList constructor.
* @param $tid
* @param $name
* @param $zone
* @param $publish_time
* @param $smallimg_url
* @param $isRented
*/
public function __construct(array $option)
{
$this->tid = isset($option["id"])?$option["id"]:null;
$this->name = isset($option["name"])?$option["name"]:null;
$this->zone = isset($option["zone"])?$option["zone"]:null;
$this->publish_time = isset($option["publish_time"])?$option["publish_time"]:null;
$this->smallimg_url = isset($option["smallimg_url"])?$option["smallimg_url"]:null;
$this->isRented = isset($option["isRented"])?$option["isRented"]:null;
}
}
程序中还可以合理利用 extract()
函数 和 compact()
函数 灵活的运用变量和数组之间的互相转化。
构造时如果已经有了所需的参数变量,所需要的
array $option
,可以使用compact()
函数获得。/** @param `$id` @param `$name` @param `$zone` @param `$publish_time` @param `$smallimg_url` @param `$isRented` 上述变量都存在且已赋值 **/ $info = compact('id','name','zone','publish_time','smallimg_url','isRented'); $thing = new Thing($info);