Take the 2-minute tour ×

So had some great feedback, went back and read through a lot on OOP. So would you say this is correct? Where I've set variables in index.php you could use pdo to query the database to grab the user data and then say loop the objects?


// This is where you could query the db?
$firstname = "Vaughan";
$lastname = "Slater";
$age = 20;

// Create two new users
$user1 = new user($firstname, $lastname, $age); // From variables
$user2 = new user("Tom", "Jones", 41); // Static set

// Output new users & could you then loop this to pull a list of users from a query
echo print_r($user1, TRUE);
echo print_r($user2, TRUE);


class user
    private $_firstname;
    private $_lastname;
    private $_age;

    public function __construct($firstname, $lastname, $age)
        $this->_firstname = $firstname;
        $this->_lastname = $lastname;
        $this->_age = $age;

I think I'm making progress, just want to make sure I'm doing doing it wrong again.. It kind of reminds me of the relationship between things like html and css.

share|improve this question
Looks okay to me. Do you have a question? –  Asaph Feb 6 at 16:39
This question would be better if asked at Code Review. Also, whether it's good or not - what happens if you have 50 entries describing your user? Or if some users have properties that some other users don't? For example, a lot of people don't have middle name. Other people have more than 2 names. Some even have 10. –  N.B. Feb 6 at 16:41
Mainly I just wanted to clarify this is the right way to do it. I've no one to ask that's knows any programming languages so this is the best place for me to ask something like this, sorry if it seems stupid. –  user3245541 Feb 6 at 16:41

1 Answer 1

up vote 1 down vote accepted

The basics are layed out, but now comes the complex part.

You'll have to query the DB. You'll get a collection to cycle over. For each item you get you'll have to copy the data into your objects and put them together into a collection (say, an array):

$users[] = new user($row['firstname'], $row['lastname'], $row['age']);

Note you can instruct with `pdo_mysql' to directly get objects instead of rows, so that copying will be done automatically.

Next, you'll cycle over that collection you have built and render it. These passages are not necessary, you can directly render the database queries result.

This is a quick pointer to a wiki showing how to use pdo_mysql.

You'll have to do those passages if you want to stratify the application, which is a good (best?) practice.

This way, each cycle will be done in a different stratum and data is passed from one stratum to the next:

persistence   (queries DB and populates $users[] array)
business      (process data according to business logic)
presentation  (render the results in HTML)

From top to bottom is the way data is passed trhough the strata.

Normally, though, the strata are shown in exactly the opposite way (bottom to top).

share|improve this answer
Thanks this is great to hear and I (think?) understand what you're saying.. would it be along these lines? code: puu.sh/6M8rB.png –  user3245541 Feb 6 at 16:59
Yes but you'll have to cycle over the result set and don't need the intermediate variables. I have added a link to some good examples (look at the pdo_mysql above). –  pid Feb 6 at 17:03
I had actually had a look at that page before, I've read through it again. Could you possibly tell me why puu.sh/6MZxD.png isn't working? I'm not sure where I've gone wrong.. the page is just white with nothing on it. –  user3245541 Feb 7 at 8:21
Never mind I was a moron and had missed an '"'! –  user3245541 Feb 7 at 11:29

Your Answer


