php - 数组到对象抛出未捕获的错误:调用未定义的方法 stdClass
问题描述
我正在尝试从代码中重建一个对象。首先我将对对象的调用分解为数组,然后将数组转换为对象,然后测试调用是否有效。
创建的对象看起来不错,但测试调用失败。
$claim = new_client();
print_r($claim);
$pat = $claim->patientFirstName();
echo $pat;
function new_client()
{
$text = '
$claim->patientFirstName()
UNWANTED STUFF.
$claim->patientMiddleName()
UNWANTED STUFF.....
$claim->patientLastName() ';
$client = array();
$i = 1 ;
$array = (explode("claim->",$text));
foreach ($array as &$variable) {
$variable = substr($variable, 0, strpos($variable, "()"));
$client[$variable] = $i;
$i++;
}
unset ($variable);
$object = new stdClass();
foreach ($client as $key => $value)
{
$object->$key = $value;
}
return $object;
}
这是结果。
stdClass Object ( [] => 1 [patientFirstName] => 2 [patientMiddleName] => 3
[patientLastName] => 4 )
Fatal error: Uncaught Error: Call to undefined method
stdClass::patientFirstName()...
所以关于为什么 $pat = $claim->patientFirstName(); 的任何想法 失败了吗?
解决方案
您正在访问元素是错误的方式。
$claim->patientFirstName
$claim->patientMiddleName
推荐阅读
- python - 如何在python中打印包含引号的字符串
- javascript - 如何将 websocket 上的新消息 .append() 到带有 CSS 样式的页面?
- python - 如何用python解析维基百科编辑数据?
- azure - Azure B2C 单一登录 (SSO) 未按预期工作
- git - 如何设置 Git 扩展和 VS2005
- r - 如何删除数据集的重复值并计算每个值重复的次数?
- memory-management - 为什么 WebAssembly 中的 malloc 需要 4 倍的内存?
- python-3.x - Wamp Server 不运行 pyscreenshot 和 pyautogui
- python - Django按序列化方法字段过滤
- mongodb - 验证后MongoDB添加数据库和用户