r/PHPhelp • • Jan 18 '11

Getting into OO PHP coding (I've been doing things procedurally) -- question about displaying results from a database and/or populating an object.

I ask because I am unsure how people do this. Displaying results (for a simple page) procedurally from a database is rather simple. It goes like this:

$conn = mysql_connect(blah blah);
mysql_select_db($dbname);

Important part: $query = "SELECT * FROM Table1 WHERE ID = 1111"; $thing = mysql_query($query); list($result1, $result2, ..., $result20) = mysql_fetch_row($thing);

Then later on, you simply insert the items like this:

<h3><?php echo $result1 ?></h3>

<p><?php echo $result2 ?></p>

etc.

Whereas, with OO code would it be something like (assuming similar object properties for database fields, and I would also prepare the statements, but for the sake of verbosity not here): (?)

<?php
class Hrm
{
    public $result1;
    public $result2;
    ...
    public $result20;
}

$dbh = new PDO(blah blah);
$query = "SELECT * FROM Table1 WHERE ID = 1111";
$res = $dbh->query($query);
$resulting_object = $res->fetchALL(PDO::FETCH_CLASS, 'Hrm');
?>

eventually to be used as:

<h3><?php echo $resulting_object->result1 ?></h3>

<p><?php echo $resulting_object->result2 ?></p>


  1. Is this right?

  2. Is it more common to generate an anonymous object (FETCH_OBJ) or key-values (FETCH_ASSOC) if we are just displaying data with nothing fancy going on? I'm guessing the major benefit to using a pre-structured class would be to access already built methods to easily manipulate the data -- right?

  3. If we wish to populate a pre-structured class, what if your object properties don't align with your database fields, and how then do you populate a pre-structured object's properties?

My apologies in advance if these are pedantic questions.

7 Upvotes

4 comments sorted by

2

u/maskapony Jan 18 '11

Normally I'd pull all of the associative values (FETCH_ASSOC) into a class variable array called $row.

Then use function __get($attribute) { return $this->row[$attribute] }

in the class to dynamically access the results from the database. Here's an example interface that would be good to aim for.

//Controller Code
$user = User::find(1);

//View code
<h1><?=$user->name?></h1>
<p><?=$user->short_profile?></p>
<p><strong>Email:</strong> <?=$user->email?></p>

1

u/[deleted] Jan 19 '11

Thanks for your response -- I found that useful. Would you say this is a common way to approach this?

(Just trying to make sure I understand OO best practices or "typical" usage in this situation)

1

u/maskapony Jan 19 '11

Yes, this is very common, it follows the principles of the ActiveRecord pattern. In it's simplest form you have a class that represents a database table and one instance of that class for each row of data in the table.

Normally, finder methods will be static, since they operate on the table as a whole with no need to instantiate. eg: $users = User::find("all"); $users = User::find_active("all"); $user = User::find("first");

There's many implementations but here's a good one if you want to look over more example code: http://www.phpactiverecord.org/projects/main/wiki/Basic_CRUD

2

u/[deleted] Jan 19 '11

DB objects are always one of the first things people attempt when venturing into oop

Fill out this template

class DB{
    public function __construct($host, $user, $pass, $database){}
    private function _connect(){}
    public function runQuery($quey){}
}

usage would look someting like this

$db = new DB(//credentials);
$results = $db->runQuery("select * from table");

foreach($results as $set){
    //do something with set
}