PHP Object Access
From w3cyberlearnings
Contents |
PHP Access Object
How to access object. In this example, we access object without convert to an array.
Example 1
<?php $object = new stdClass(); $object->name = "Bob Markina"; $object->age = 24; foreach($object as $obj) { echo $obj ; echo "<br/>"; } ?>
Output
Bob markina 24
Example 2
<?php $employee_obj = new stdClass(); $employee_obj->infor->name = 'Jamy Jo'; $employee_obj->infor->age = 58; $employee_obj->address->street = '240 Lake Rd'; $employee_obj->address->apartment = '240'; $employee_obj->address->state = 'Tx'; $employee_obj->address->city = 'Houston'; $employee_obj->address->zip = '77300'; $employee_obj->infor->role = 'PHP Developer'; // access address foreach ($employee_obj->address as $obj) { echo $obj; echo "<br/>"; } echo '<br/>'; /* 240 Lake Rd 240 Tx Houston 77300 */ // access infor foreach ($employee_obj->infor as $obj) { echo $obj; echo "<br/>"; } /* Jamy Jo 58 PHP Developer */ ?>
Output
240 Lake Rd 240 Tx Houston 77300 Jamy Jo 58 PHP Developer
Related Links
--PHP Object to Array-- PHP Object to JSON-- PHP Access Object--