Jump to content

When i make classes which select and display data, which way is better ? (below)


jd2007

Recommended Posts

When i make classes which select and display data from a database, which way is better ? i usually make a function to select data from a database and another function to display the data ? is this good practice or no ? below is a clear example:

 

<?php
class Data
{

funtion select()
{
   // select data from the database
}

function display()
{
  // display selected data
}

}
?>

Link to comment
Share on other sites

I think you should keep your objects single-tasked.

 

Your database class' job is to get data from the database, not to display it.

 

Say you're executing more than 1 query.. why should your display() function have to care about which table it's pulling from, ever? It would have to if you want to use 1 function to display.

 

Now, i could see an argument being made for display() to be a private function whose sole purpose is to print out debug information. But, this is what __toString() is for :)

 

Does that make sense?

Link to comment
Share on other sites

All functions in classes should be self contained and if possible, not echo or print anything but return values that can be printed later.

 

example

 

class db {
function getData () {
  //getData from db,
  return $data

}
}


$db = new db;
$data = $db->getData;
echo $data;


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.