2
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

抱歉,我尽量解释这个问题。谢谢各位的回答!!

4

3 回答 3

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;
于 2012-12-31T06:44:06.200 回答
1

您正在尝试将属性作为数组元素访问。您需要扩展 ArrayObject。http://php.net/manual/en/class.arrayobject.php来做到这一点。否则不要混合对象和数组。

于 2012-12-31T06:38:39.190 回答
0

文件 'Property.php'
添加属性
$address_primary = "";
$defaultAddress = "这是要测试的临时地址";
函数 __construct($primaryAddress = ""){
  this->address_primary = (strlen(trim($primaryAddress)) > 0)?
  $primaryAddress :$defaultAddress;  
}

于 2012-12-31T06:47:28.480 回答