Jump to content

Need advice on how to best code.


OM2

Recommended Posts

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

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" />

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.