include('Property.php');
$obj = new Property ();
$obj->price = 2500.00;
$obj ['address_primary'] = '100 Main St';
$obj->state = 'VA';
echo 'Address :: ', $obj->address_primary, ' ', PHP_EOL;
echo 'City, State, Zip :: ', $obj ['state'];
可以请有人向我解释如何:从$ obj = new Property()我们可以创建/初始化或者:$ obj ['address_primary']并且能够回应这个:$ obj-> address_primary
抱歉,我尽可能多地解释这个问题。谢谢你的回答伙计!!
答案 0 :(得分:1)
您正尝试将属性作为数组元素进行访问。您需要扩展ArrayObject。 http://php.net/manual/en/class.arrayobject.php这样做。否则不要混合对象和数组。
答案 1 :(得分:1)
你应该可以改变
$obj ['address_primary'] = '100 Main St';
到
$obj->address_primary = '100 Main St';
您还必须更改
echo 'City, State, Zip :: ', $obj ['state'];
到
echo 'City, State, Zip :: ', $obj->state;
答案 2 :(得分:0)
档案'Property.php'
添加财产
$ address_primary =“”;
$ defaultAddress =“这是要测试的临时地址”;
funciton __construct($ primaryAddress =“”){
this-> address_primary =(strlen(trim($ primaryAddress))> 0)?
$ primaryAddress:$ defaultAddress;
}