Xeoncross Posted October 4, 2007 Share Posted October 4, 2007 I have been working on a plugin system for a while now, and as I get farther along I am starting to notice WAY too many files being included (try 83) just to render a blog post. Now it is not that I have 83 different files that NEED to be included - but I have several that are included REPEATEDLY in order to render a page. Files like "comments.php" that is included every time a comment is placed in the layout (imagine a post with 100 comments!). Here is an example of what is going on in my script: <?php ...(CODE)... while ($row = mysql_fetch_assoc($result)) { //Include the file that will place the variables in the HTML correctly... include("comments.php"); } ...(CODE)... ?> and then comments.php looks like this: <h2><?php print $row['title']; ?></h2> <div><?php print $row['text']; ?></div> So I want to know what you thing about maybe doing something like instead of including the same file over and over - just include it once and use str_replace() to enter the new values for each comment. But I am not sure how I would do that. Or is there another way I could handle this problem? The two most important things are speed and I would like to still be able to use PHP code in the files like "comments.php". Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/ Share on other sites More sharing options...
Xeoncross Posted October 4, 2007 Author Share Posted October 4, 2007 A friend just recommended that I try placing the comments.php code in a function that can be included once but called any number of times! So comments.php would look like this: function place_data_in_comment($row) { <h2><?php print $row['title']; ?></h2> <div><?php print $row['text']; ?></div> } Does anyone know of any problems with this approach? Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-362033 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 That is a much better approuch but seriously, breaking up your code into tiny little pieces like that is going to make the logic harder to follow. Its good to braking you code into smaller chunks but I really think your taking it to the extreme. Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-362047 Share on other sites More sharing options...
Xeoncross Posted October 4, 2007 Author Share Posted October 4, 2007 Its good to braking you code into smaller chunks but I really think your taking it to the extreme. lol that is true However, the examples shown are simplified versions of their real-life counterparts. Plus, I can't hard-code a file like 'comments.php' into a file, because not only is it a template file that designers need to edit - it is also a plugin that isn't a part of the system core. But I get your drift. Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-362052 Share on other sites More sharing options...
Xeoncross Posted October 5, 2007 Author Share Posted October 5, 2007 Well, I have run some tests and I found that including a file once and then running a function to place the code is about 5x faster than including the file over and over. If you want to run the test yourself I have attached a zipped version of the 3 files I used to test with - upload them to the same place a run "index.php". [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-362180 Share on other sites More sharing options...
web_design_sydney Posted October 5, 2007 Share Posted October 5, 2007 You could make it a variable. The variable is set once and then referenced X times - <?php ...(CODE)... while ($row = mysql_fetch_assoc($result)) { //Include the file that will place the variables in the HTML correctly... include("comments.php"); $title = $row['title']; } ...(CODE)... ?> Depending on what you have in $result (obviously has to include row title) and then comments.php looks like this: <h2><?php '$title'; ?></h2> Saves you from performing the same row query many times. Could make the comment var i++ for 'X' for id and get for str() to give each var a unique id. So - $commentX = string Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-362214 Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 When coding in C you will often see this type of thing in header files. Pretend this file is called header.h. #ifndef HEADER_H #define HEADER_H // all the header code #endif This ensures that if a header is included more than once, the code only sees the light of day once. You could do the same thing with PHP like so. <?php if(!defined(HEADER_INC_PHP)) { define('HEADER_INC_PHP', true); // all your header code } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-362457 Share on other sites More sharing options...
MmmVomit Posted October 5, 2007 Share Posted October 5, 2007 <?php ...(CODE)... while ($row = mysql_fetch_assoc($result)) { //Include the file that will place the variables in the HTML correctly... include("comments.php"); } ...(CODE)... ?> Using includes like this is fundamentally wrong. You want something like this. <?php include("comments.php"); ...(CODE)... while ($row = mysql_fetch_assoc($result)) { //Include the file that will place the variables in the HTML correctly... this_is_a_function_defined_within_coments_php(); } ...(CODE)... ?> Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-362460 Share on other sites More sharing options...
TomKrush Posted October 9, 2007 Share Posted October 9, 2007 Also another important note, in the future remember that the server slows down when you import many files. So your example was a 100 comments. Well that is going to import comments.php 100 times as you know. I would hate to imagine how many files you import on each execution of the code. Speed and efficient code is so important especially when you systems become large. Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-365726 Share on other sites More sharing options...
Jenk Posted October 9, 2007 Share Posted October 9, 2007 include_once() ? Quote Link to comment https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/#findComment-365754 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.