Work with PHP objects

I used to be the main script in PHP and am now considering whether to "seriously":

I am working on a hiking site, and I needed to put some values ​​in an object, which I then try to pass back to the calling code.

I tried to do this:

$trailhead = new Object (); 

But system sorting is on me.

Then I did not declare the object at all and started using it as follows:

 $trailhead->trailhead_name = $row['trailhead_name']; $trailhead->park_id = $row['park_id']; 

It seemed to work fine. But there are at least 3 problems:

  • Since this code runs when getting things from the database, what should I do if there is more than one row?

  • When I passed $ trailhead back to the calling code, the variables were empty

  • Actually, I might be better off making a real object for Trailhead as follows:

     class Trailhead { private $trailhead_name; private $park_id; private $trailhead_id; public function __construct() { $this->trailhead_name = NULL; $this->park_id = NULL; $this->trailhead_id = NULL; } } 

What do people usually do in these situations and where am I wrong in my approach? I know him more than one place :)

+4
source share
3 answers
  • $trailheads[] = $trailhead;
  • I would do print_r() from $trailhead to check what you expect from it. The default object type in PHP will be stdClass .
  • Yes, it will be better, as it will allow your Trailhead objects Trailhead have functions. The way you do it now basically does not use any of the functionality of the PHP object - it is essentially an array with slightly different syntax.
+4
source

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.

+3
source

Just return the array:

 $trailhead = array( 'trailhead_name' => $row['trailhead_name'], 'park_id' => $row['park_id'], 'trailhead_id' => $row['trailhead_id'], ) 

Then either refer to it like this:

 $trailhead['park_id']; 

or use the excellent list() to read it in variables:

 list($trailhead_name, $park_id, $trailhead_id) = $trailhead; 
+1
source

All Articles