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'];

Can please someone Explain to me how, from having this : $obj = new Property () we can create/initialize or have : $obj ['address_primary'] and be able to echo this : $obj->address_primary

Sorry but I try to explain the problem as much as i could. Thanks for your answers Folks!!

2
  • 1
    does Property extend ArrayAccess? If not, you shouldn't be doing stuff like $obj['address_primary'], it will always be $obj->address_primary Commented Dec 31, 2012 at 6:37
  • Property class code please Commented Dec 31, 2012 at 6:37

3 Answers 3

1

You are trying to access a property as an array element. You need to extend ArrayObject. http://php.net/manual/en/class.arrayobject.php to do this. Otherwise dont mix objects and arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

By Extending the class 'Property' to ArrayObject(), it should take care of this as well, right? : $obj->price = 2500.00; echo $obj ['state'];
@Blingue no prob, dont forget you can vote up answers that helped! ;)
1

You should be able to just change

$obj ['address_primary'] = '100 Main St';

to

$obj->address_primary = '100 Main St';

You'll also have to change

echo 'City, State, Zip :: ', $obj ['state'];

to

echo 'City, State, Zip :: ', $obj->state;

Comments

0

File 'Property.php'
Add property
$address_primary = "";
$defaultAddress = "This is temp address to test";
funciton __construct($primaryAddress = ""){
  this->address_primary = (strlen(trim($primaryAddress)) > 0)?
  $primaryAddress :$defaultAddress;  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.