Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with PHP objects

Tags:

php

I was previously mostly scripting in PHP and now considering getting "more serious" about it :)

I am working on a hiking website, and I needed to put some values into an object that I then try to pass back to the calling code.

I tried doing this:

$trailhead = new Object ();

But the system sort of barfed at me.

Then I didn't declare the object at all, and started using it like this:

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

That seemed to work reasonably ok. But there are at least 3 problems with this:

  1. Since that code gets executed when getting things from the database, what do I do in case there is more than one row?

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

  3. I actually am maybe better off making a real object for Trailhead like this:

    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 generally do in these situations and where am I going wrong in my approach? I know its more than one place :)

like image 817
Genadinik Avatar asked Feb 24 '23 23:02

Genadinik


2 Answers

  1. $trailheads[] = $trailhead;
  2. I'd do a print_r() of $trailhead to check that it's what you expect it to be. The default object type in PHP is going to be stdClass.
  3. Yes, that's going to be better, as it'll allow your Trailhead objects to have functions. The way you're currently doing it is basically taking advantage of none of PHP's object functionality - it's essentially an array with slightly different syntax.
like image 83
ceejayoz Avatar answered Feb 26 '23 20:02

ceejayoz


I think you should get in "contact" with some of the basics first. Objects in PHP have sort of a history. They are a relative to the array and there are two sorts of objects:

  • data-objects
  • class objects

data objects are called stdClass and that's actually what you were initially looking for:

$trailhead = new Object();

in PHP is written as:

$trailhead = new stdClass;

(with or without brackets at the end, both works)

You then can dynamically add members to it, like you did in the second part without declaring the variable (that works, too in PHP):

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

If you want to more quickly turn $row into an object, just cast it:

$trailhead = (object) $row;

That works the other way, too:

$array = (array) $trailhead;

As you can see, those basic data based objects in PHP do not hide their relationship to the array data type. In fact you can even iterator over them:

foreach($trailhead as $key=>$value) {
    echo $key, ' is ', $value, "<br>\n";
}

You can find lots of information about this in the PHP manual, it's a bit spread around, but worth to know the little basics before repeating a lot of names only to pass along the data that belongs together.

Next to these more or less stupid data objects, you can code complete classes that - next to what every stdClass can do - can have code, visibility, inheritance and all the things you can build nice stuff from - but this can be pretty complex as the topic is larger.

So it always depends on what you need. However, both type 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 merely does the same in each object (store data), you should just take one flexible class that is handling the job flexible (e.g. stdClass) because that will keep your code more clean.

Instead of coding a getter/setter orgy you can then spend the time thinking about how you can make the database layer more modular etc. .

like image 25
hakre Avatar answered Feb 26 '23 21:02

hakre