jimmyoneshot Posted May 11, 2010 Share Posted May 11, 2010 Ok this may sound strange but I'm wondering what is considered best practice, to have as many php files, each handling a single query or to have a single huge php file which handles multiple queries. Basically I mean what is better less or more and is there an advantage to either performance or anything else-wise? At the moment I tend to use php files which retrieve data as their own php files i.e. I have a php file which gets all categories from my categories table then outputs this as xml and another php file which retrieves all items from my items table and outputs that data as xml. But when it comes to inserting/updating/deleting data in a table I try to use if statements within 1 big php file based on a variable which I've named 'requesttype' which is basically what type of request the user has made eg if they click add the add php code will be used if they click update the update code etc. Am I doing this along the right lines and is less indeed more when it comes to the amount of php files? Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/ Share on other sites More sharing options...
frenchpl Posted May 11, 2010 Share Posted May 11, 2010 Hi there, I will have a go at this and leave it open for others to follow. No doubt there will be a whole raft of answers telling me that I am wrong but here goes. From the way you phrase your question, it would seem that you code is 'data driven'. I would seriously suggest a look at object oriented coding and follow where that leads you. Pete. Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056842 Share on other sites More sharing options...
Jiblix Posted May 11, 2010 Share Posted May 11, 2010 Well, for me personally I try to use as little PHP files as possible. Each executing as little code as possible. For example you dont want a file called "Functions.php" That you contain thousands of functions in when the page you are loading only needs one of those functions. Because of that your loading lets say.. an extra 15,000 lines of code that never get executed. Keep your code simple. And dont include things that are not relative to the script that is being run. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056843 Share on other sites More sharing options...
roopurt18 Posted May 11, 2010 Share Posted May 11, 2010 If your server has an optimizer installed then it really doesn't matter how many or how few files are accessed or used by your PHP application. However it sounds like your programs all execute within a global namespace without much use of functions or objects. That style of programming tends to lead to buggy programs. Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056845 Share on other sites More sharing options...
jimmyoneshot Posted May 11, 2010 Author Share Posted May 11, 2010 Thanks for the replies. To better explain what I mean here's a bit of my code from a file I've made called userrequest.php:- if(($param_requesttype == "add") && ($param_loggedinuserid != "")){ $query = "UPDATE userlinks SET favourited = '1' WHERE linkid = '$param_linkid' AND userid = '$param_loggedinuserid'"; if ( !mysql_query($query, $mysql_connection) ){ die('ERROR: '. mysql_error() ); } echo "LINK FAVOURITED"; } else if(($param_requesttype == "remove") && ($param_loggedinuserid != "")){ $query = "UPDATE userlinks SET favourited = '0' WHERE linkid = '$param_linkid' AND userid = '$param_loggedinuserid'"; if ( !mysql_query($query, $mysql_connection) ){ die('ERROR: '. mysql_error() ); } echo "LINK UNFAVOURITED"; } else if(($param_requesttype == "savetwitterusername") && ($param_loggedinuserid != "")){ $query = "UPDATE users SET twitterusername = '$param_twitterusername' WHERE userid = '$param_loggedinuserid'"; if ( !mysql_query($query, $mysql_connection) ){ die('ERROR: '. mysql_error() ); } echo "TWITTER USERNAME UPDATED"; } My application itself allows users to do many things but the three things that are applicable to the above are that it allows users to add links to favourites within my application, remove links from favourites and save their twitter username. The above code used to be seperated into 3 different php files called addlink.php removelink.php and twitterservice.php but I've now condensed these into one file through the use of if loops as you can see in the example above. Is this better practice is basically what I'm wondering? Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056849 Share on other sites More sharing options...
roopurt18 Posted May 12, 2010 Share Posted May 12, 2010 Ooooh boy. You're in for some hurt with that script. Instead of checking $param_loggedinuserid != "" every time, why not just have this at the top of your page: if( $param_loggedinuserid != "" ) { return; // or exit(); } You're not using mysql_real_escape_string() anywhere. You're using die() and sending mysql errors directly to the browser. Someone is going to attack your site eventually if you keep that stuff up. Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056856 Share on other sites More sharing options...
jimmyoneshot Posted May 12, 2010 Author Share Posted May 12, 2010 Yep thats a good point and I may try that cheers. So just basically a big if loop surrounding all of the other if loops? The thing is it's an Adobe Air desktop application that I'm using this for it only retrieves info via these php files it's not a website. But besides that would you say the code is pretty effiecient in the way I've done it i.e. with mulitple if loops (or statements as I'm not sure if they're referred to as "if loops") rather than having multiple php files? Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056859 Share on other sites More sharing options...
roopurt18 Posted May 12, 2010 Share Posted May 12, 2010 I couldn't even begin to guess as to how efficient PHP runs inside of Adobe Air. Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056890 Share on other sites More sharing options...
jimmyoneshot Posted May 12, 2010 Author Share Posted May 12, 2010 Ah right. No worries. I basically have a bunch of php files posted on a server and my application posts requests to these files which either does an update, insert, or delete to the database tables or gets the data from these tables in this database and outputs them as xml so this info can be displayed within my app depending on the type of request a user submits. The whole thing is pretty cool. So if you were to do this how would you do it in your view? I mean if you needed to insert, delete and update a table would you recommend just 1 php file to do them all or should they be split into 3 files? What would operate most efficiently/is best practice regardless of whether it's in air or on a website as I imagine it's pretty much the same. That's all I want to know really Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056895 Share on other sites More sharing options...
roopurt18 Posted May 12, 2010 Share Posted May 12, 2010 All of my programs are built around MVC for the most part. So essentially there is one file per abstract database object. Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056897 Share on other sites More sharing options...
jimmyoneshot Posted May 12, 2010 Author Share Posted May 12, 2010 Gotchya. Cheers for the suggestions anyway roopurt. Quote Link to comment https://forums.phpfreaks.com/topic/201430-question-about-amount-of-php-files-in-general-good-and-bad-points/#findComment-1056900 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.