OM2 Posted July 30, 2008 Share Posted July 30, 2008 I've started off witha big HTML file. I've split off different chunks into seperate PHP files and have included these. Doing this is brilliant and allows me to make changes independentaly of the rest of the page. But: as my page gets bigger, I can see things getting more complex. One future problem I can see is trying to figure out how everything happens. For example, in one file, variables are declared and then used in another file. When I come back to the code several months after not looking: I know I'll be confused and will have to trace back! Not sure if any of the above makes sense! Am I doing the right thing splittng up the file in the first place? Any advice would be great. Thanks. OM Link to comment https://forums.phpfreaks.com/topic/117433-need-advice-on-how-to-best-code/ Share on other sites More sharing options...
realjumper Posted July 30, 2008 Share Posted July 30, 2008 It's hard to say without seeing everything, but if you add comments to your code to remind you what each piece of code does, then you shouldn't have a problem Link to comment https://forums.phpfreaks.com/topic/117433-need-advice-on-how-to-best-code/#findComment-604035 Share on other sites More sharing options...
ronnie88 Posted July 30, 2008 Share Posted July 30, 2008 if i was writing a huge page i'd put the biggest php parts into another file and include them and after the include just use something like this include("name"); //does what it does sure that will help later on. Link to comment https://forums.phpfreaks.com/topic/117433-need-advice-on-how-to-best-code/#findComment-604037 Share on other sites More sharing options...
OM2 Posted July 30, 2008 Author Share Posted July 30, 2008 thanks for the replies. i'll make sure i comment well so i dont get lost in future. Link to comment https://forums.phpfreaks.com/topic/117433-need-advice-on-how-to-best-code/#findComment-604040 Share on other sites More sharing options...
littlened Posted July 31, 2008 Share Posted July 31, 2008 If it helps. I tend to have the header/footer etc as includes, and any other little bits. What I do then is have a php file for each page. So say I have a page which lists products, I would have a page which has the look of the page in it, and right at the top it would include a php file like 'php/php_productlist.php'. If I had db content in the header, I would have a file called 'php/php_footer.php' which would be includes in the file 'inc/inc_footer.php'. I find doing it this way helps to separate content from code, creating a sort of code behind page. My folder structure usually looks like... > classes - for any class files cls_db.php > css - for css files i.e. css_mysite.css > inc - for include files i.e. inc_header.php > php - for PHP files i.e. php_productlist.php > images - images obviously here's a brief example. productlist.php <?php include('php/php_productlist.php'); ?> <html> <head> </head> <body> <?php include('inc/inc_header.php'); ?> <h1>My page title</h1> <p>you can echo out values set in the file 'php/php_productlist.php' in here</p> <?php include('inc/inc_footer.php'); ?> </body> </html> php/php_productlist.php <?php // get a list of products from the database and store it in an array ?> inc/inc_header.php - again you can you can put PHP code in an include file for the header as well <?php include('php/php_header.php'); ?> <h2>Welcome to my site</h2> <img src="mylogo.png" /> Link to comment https://forums.phpfreaks.com/topic/117433-need-advice-on-how-to-best-code/#findComment-604295 Share on other sites More sharing options...
samshel Posted July 31, 2008 Share Posted July 31, 2008 i think this is a very good example of procedural programming and you are bound to come across problems when the file grows huge... I think you should use OOPs, encapsulate entities, create Classes and use them.. for example: Current scenario include "header.php"; //include header //some code include "showproductlist.php"; //shows product list.. //some code include "showproductdetails.php"; //shows product details.. //some code using OOPs, you can define product as one entity and create a class for it //define class in some other file and include it in your file. Class product() { function showproductlist() {} function showproductdetails() {} } include "product.php"; //include product class include "header.php"; //include header //some code $objProduct->showproductlist() //some code $objProduct->showproductdetails() //some code this way you club code related to one entity like product in one class and is easy to change and is reusable... Link to comment https://forums.phpfreaks.com/topic/117433-need-advice-on-how-to-best-code/#findComment-604315 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.