Iterate over an object with private attributes

I am using PHP with an active php entry. When I retrieve a record from the database, the attributes are listed as private. I need to iterate over the attributes and get a key => value pair. How can this be done?

$row = \Models\Locations::find(2); Models\Locations Object ( [errors] => [attributes:ActiveRecord\Model:private] => Array ( [id] => 2 [customer_id] => 6 [name] => test location [address_line1] => 123 test Drive [address_line2] => [city] => Moon Township [state] => AZ [zip] => 12345 [country] => USA [primary_phone_number] => 123.456.7890 [latitude] => 0 [longitude] => 0 [coordinate_precision] => ) [__dirty:ActiveRecord\Model:private] => Array ( ) [__readonly:ActiveRecord\Model:private] => [__relationships:ActiveRecord\Model:private] => Array ( ) [__new_record:ActiveRecord\Model:private] => ) 
+4
source share
3 answers

Just use the attributes model method: $row->attributes()

+2
source

If you want to iterate over everything, then you will need to implement the Iterator interface.

 class ModelExtended extends Model implements Iterator { // implement the Iterator interface within - then extend this class instead of Model } 
+1
source

You cannot, unless you have a publicly accessible getter function that allows you to get an array.

+1
source

Source: https://habr.com/ru/post/1442819/


All Articles