Jump to content

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


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
}

}
?>

U can modify it 4 better use,

 

<?php

class Data

{

function connect_db($db_name)

{

}

 

funtion select($table_name,$where_condn)

{

  // select data from the database

}

 

function display($resultset)

{

  // display selected data

}

 

}

?>

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?

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;


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.