jd2007 Posted August 22, 2007 Share Posted August 22, 2007 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 } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66139-when-i-make-classes-which-select-and-display-data-which-way-is-better-below/ Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 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 } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66139-when-i-make-classes-which-select-and-display-data-which-way-is-better-below/#findComment-330830 Share on other sites More sharing options...
jd2007 Posted August 23, 2007 Author Share Posted August 23, 2007 i know that...thanks Quote Link to comment https://forums.phpfreaks.com/topic/66139-when-i-make-classes-which-select-and-display-data-which-way-is-better-below/#findComment-331641 Share on other sites More sharing options...
keeB Posted August 23, 2007 Share Posted August 23, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/66139-when-i-make-classes-which-select-and-display-data-which-way-is-better-below/#findComment-331658 Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/66139-when-i-make-classes-which-select-and-display-data-which-way-is-better-below/#findComment-331663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.