top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Best way of iterating over object member fields in PHP?

0 votes
478 views

I came across a bug in some of my code, where I do something like the following:

$obj = new MyObject();

foreach ($obj as $key=>$value)

{    
 echo $key;
}

Note that I don't use $value in the loop - I'm only doing processing based on the field name.

Now my IDE flags this unused variable as a hint, so (on autopilot) I changed it to be

$obj = new MyObject();
foreach (array_keys($obj) as $key)
{
 echo $key;
}

Which removes the hint, and looks like the right sort of thing. Of course this doesn't work: "array_keys() expects parameter 1 to be an array, object found".

So what is the correct way to list the field names of an object?

posted Sep 3, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Two ways depending on whether you want to get them from the class definition or what the object actually contains:
* http://php.net/get-class-vars
* http://php.net/get-object-vars

1 Answer

+1 vote
 
Best answer

You could use get_object_vars
http://php.net/manual/en/function.get-object-vars.php

So something like the following may work

$obj = new MyObject();
foreach (array_keys(get_object_vars($obj)) as $key)
{
 echo $key;
}
answer Sep 3, 2014 by Sonu Jindal
Similar Questions
+1 vote

I want to code a member area script for processing activities between members of my website i.e. sending and receiving messages between logged in members. Any suggestions?

0 votes

I am trying to export certain data from my PHP form to CSV. I can echo out to screen during testing and I can also export to CSV the static test data (stored in the $contents array) you see below. But I am stuck trying to export the certain fields that I only need to export.
This is my code

// How do I get this info into the CSV?
/*foreach ( $entries as $entry ) :  
    echo $entry['2'];
    echo $entry['3'];
    echo $entry['6'];
endforeach;*/

$csv_headers = [
    'Organisation Name',
    'Registered Charity Number',
    'Address',
    'Phone',
];

$contents = [
  [2014, 6, '1st half', 'roland@fsjinvestor.com', 0, 0],
  [2014, 6, '1st half', 'steve@neocodesoftware.com', 0, 0],
  [2014, 6, '1st half', 'susanne@casamanager.com', 0, 0],
  [2014, 6, '1st half', 'tim', 0, 0]
];

fputcsv($output_handle, $csv_headers);

foreach ( $contents as $content) :
    fputcsv($output_handle, $content);
endforeach;
+2 votes

I am working on building an api client for the Rakuten Market-place. I have got some test requests for add delete update etc working and thought I should aim to structure it as a Gem and publish it so that others can use it/enhance it.

I haven't built a Gem before (worked mostly within the rails environment to date).

Have been reading and looking at other api client gems and am making progress on building something (still got a way to go to handle errors etc.)

To make the gem general purpose though, I am trying to figure out the best way to provide the mapping between models in a rails app and the api client objects (such as product, category, order etc).

I am aiming to make each client api object a class be (or should i call them models?)
Then I suspect I will use new to build an api instance from a rails instance, and find to return an api instance to a rails instance.

I can build in mapping for my own models to the api objects with no problem, but I can't see how I could generalize this so that other apps with similar models could use the gem.

I suspect there are approaches for doing this, but so far I haven't managed to come up with how to do it. If anyone has any wisdom on the matter I would be grateful.

...