I think you should first get in touch with some of the basics. Objects in PHP have a kind of history. They belong to an array and there are two types of objects:
- data objects
- class objects
the data objects are called stdClass , and this is actually what you were originally looking for:
$trailhead = new Object();
in PHP it is written as:
$trailhead = new stdClass;
(with or without brackets at the end, both work)
Then you can dynamically add participants to it, as in the second part, without declaring a variable (which also works in PHP):
$trailhead->trailhead_name = $row['trailhead_name']; $trailhead->park_id = $row['park_id'];
If you want to turn $ row into an object faster, just cast it:
$trailhead = (object) $row;
This also works:
$array = (array) $trailhead;
As you can see, these basic data-based objects in PHP do not hide their relationship to the data type of the array. In fact, you can even iterate over them:
foreach($trailhead as $key=>$value) { echo $key, ' is ', $value, "<br>\n"; }
You can find a lot of information about this in the PHP manual, it is a bit scattered, but you should know a little basics before repeating a lot of names just for transferring data that belong together.
Next to these more or less stupid data objects, you can encode complete classes that - next to what every stdClass can do - can have code, visibility, inheritance, and everything you can build from good stuff - but it can be pretty complicated as the topic is bigger .
Thus, it always depends on what you need. However, both types of objects are βreal objects,β and both should work.
class myClass { function importArray(array $array) { foreach($array as $key=>$value) { if(!is_numeric($key)) $this->$key=$value; } } function listMembers() { foreach($this as $key=>$value) { echo $key, ' is ', $value, "<br>\n"; } } } $trailhead = new myClass(); $trailhead->importArray($row); echo $trailhead->park_id;
Keep in mind that instead of creating a set of objects that just does the same in each object (store data), you should just take one flexible class that handles the job flexible (like stdClass), because it will keep your code cleaner .
Instead of coding a getter / setter orgy, you can spend time thinking about how to make the database layer more modular, etc.