将数组转换为自定义对象

时间:2016-10-26 23:32:01

标签: php laravel

我有一个与我的ChampionshipSettings Class

的属性相匹配的数组

所以,我需要我的数组作为ChampionshipSetting对象

我唯一能做的就是:

 $settings = $championship->settings ??  (object)$mySettings;

但是$ settings是一个Object,而不是ChampionshipSettings对象

如果我尝试:

$settings = $championship->settings ??  (ChampionshipSettings)$mySettings;

我收到语法错误....

任何想法???

1 个答案:

答案 0 :(得分:2)

您可以创建一个新对象:

$settings = $championship->settings ?? new ChampionshipSettings($mySettings);