Structuring
Moderator: General Moderators
Structuring
Which structuring method is good?
I am thinking of making a folder for each link. Suppose i have links: Category, Products, Members, Orders - each links will have add, edit, view, paging, delete functionality,
index.php
category - folder
add_cat
edit_cat
view_cat
delete_cat
page_cat
cat_big_image - folder
Same for Products, Members
In many tutorials, all the above functionality are written in a single file for each link. If i write it in a single file, the code increases and sometimes(if the form having many fields) looks ugly. Is there any disadvantage in creating folder for each links? will it slow down the site as the files will be inside folders and takes time to access them?
Thanks
I am thinking of making a folder for each link. Suppose i have links: Category, Products, Members, Orders - each links will have add, edit, view, paging, delete functionality,
index.php
category - folder
add_cat
edit_cat
view_cat
delete_cat
page_cat
cat_big_image - folder
Same for Products, Members
In many tutorials, all the above functionality are written in a single file for each link. If i write it in a single file, the code increases and sometimes(if the form having many fields) looks ugly. Is there any disadvantage in creating folder for each links? will it slow down the site as the files will be inside folders and takes time to access them?
Thanks
Re: Structuring
What do you mean by "links"? It sounds like maybe you're talking about dividing your script code into script files, but you're confusing me with talking about folders. There's no performance loss in using folders, but there could be in loading very large script files that contain code that includes a lot that is never used for any one load. I would say the advantages of folders are entirely in how convenient it is for the programmer to work on them.
Re: Structuring
Thanks.
folders for each script file. For example, Category may be a link it will have add, edit, delete, view and paging functionality. Instead of creating a single file for 5 conditional statements, i am thinking of using 5 files. As this will create many files i would be keeping them in separate folders. In tutorials all the above functionality are handled in a single file either using conditional statements or switch cases.
folders for each script file. For example, Category may be a link it will have add, edit, delete, view and paging functionality. Instead of creating a single file for 5 conditional statements, i am thinking of using 5 files. As this will create many files i would be keeping them in separate folders. In tutorials all the above functionality are handled in a single file either using conditional statements or switch cases.
Re: Structuring
So you're saying one folder per file? I see no point in doing that. As to splitting a script into multiple files, I would say it's not worth the trouble unless your routines are extremely large. Different programmers may have different ideas on this, but I think it helps to have related routines in the same file because very often changes in one part of the routine, such as changing a variable name, are easier to manage when they are together; also, I find it easier to understand the flow logic when they are together. But that's probably a preference that wouldn't be shared by everyone. In theory, splitting alternative branches of a script into separate files would result in faster loading, since you would only load one of your 5 files, as required, instead of all 5 every time. But I think the difference wouldn't be noticeable unless, as I said, the combined file was extremely large.thatsme wrote:Thanks.
folders for each script file. For example, Category may be a link it will have add, edit, delete, view and paging functionality. Instead of creating a single file for 5 conditional statements, i am thinking of using 5 files. As this will create many files i would be keeping them in separate folders. In tutorials all the above functionality are handled in a single file either using conditional statements or switch cases.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Structuring
I am not clear how creating more more files AND a folder for each of them is going to be less confusing than just more files? In general, the practice in PHP is going the opposite direction -- which is to use mod_rewrite for clean URLs and to group related request handlers into Action controllers.
(#10850)
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Structuring
I agree with arborint. You may want to think your app through to see what ways your app would benefit from something like this.
Re: Structuring
Everah | Please use appropriate bbCode tages when posting code in the forums. For monospace code text, use [code]. For a specific language, including PHP, use [code={lang}] where lang is the lowercase name of the language you want to use.
As you can see even if the form consists of five fields a minimum of 30 lines of code is needed. I want to reduce the code in each page.
Everah | Please use appropriate bbCode tages when posting code in the forums. For monospace code text, use [code]. For a specific language, including PHP, use [code={lang}] where lang is the lowercase name of the language you want to use.
Code: Select all
$condition = $_REQUEST['condition'];
if($condition=='')
$condition='paging';
if($condition=='submit')
{
//validation and insert or update statement
}
if($condition=='form')
{
//form is displayed
}
if($condition=='view')
{
//form in new mode
}
if($condition=='delete')
{
//delete statement
}
if($condition=='paging')
{
//code for paging;
}
Everah | Please use appropriate bbCode tages when posting code in the forums. For monospace code text, use [code]. For a specific language, including PHP, use [code={lang}] where lang is the lowercase name of the language you want to use.
Re: Structuring
I think what we're all saying is that you're worrying about the wrong things. Your goal of reducing the number of lines in a script doesn't seem to be based on any sound reasoning and might even be counterproductive. Another way of saying it is, "if it ain't broke, don't fix it."
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Structuring
You are trying to build a controller that is handling routing for you. This is not a new concept, but it can get hairy if you don't plan this out right.
Something you could do (and this is only an example) is:
This is only one of many ways to achieve what you want. Put some thought into it and see what you can come up with.
Something you could do (and this is only an example) is:
Code: Select all
<?php
$condition = $_REQUEST['condition'];
$file = 'module.'.$condition.'.php';
if (file_exists($file)) {
// If there is a known file get that one
require_once $file;
} else {
// Otherwise use this as a default
require_once 'module.paging.php';
}
?>This is only one of many ways to achieve what you want. Put some thought into it and see what you can come up with.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Structuring
Reducing code is a practice -- not a goal. Goals are things like: implementing the required functionality, reducing maintenance, or meeting some resource constraints. Solve the problem first ... then optimize.thatsme wrote:As you can see even if the form consists of five fields a minimum of 30 lines of code is needed. I want to reduce the code in each page.
(#10850)