Thousands of lines of code in one PHP file.....

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
pgolovko
Forum Commoner
Posts: 38
Joined: Sun Sep 17, 2006 9:13 am

Thousands of lines of code in one PHP file.....

Post by pgolovko »

The PHP file is almost 300 Kb in size with thousands of lines of code, in structure similar to:

Code: Select all

switch ($type) {
	case 1:
		//LOTS OF CODE
	break;
	case 2:
		//LOTS OF CODE
	break;
	case 3:
		//LOTS OF CODE
	break;
	case 4:
		//LOTS OF CODE
	break;
	case 5:
		//LOTS OF CODE
	break;
}
What would be the advantages and disadvantages of breaking it up into:

Code: Select all

switch ($type) {
	case 1:
		require_once("code1.php");
	break;
	case 2:
		require_once("code2.php");
	break;
	case 3:
		require_once("code3.php");
	break;
	case 4:
		require_once("code4.php");
	break;
	case 5:
		require_once("code5.php");
	break;
}
The code is being added every day, so the PHP file will grow to 400 Kb soon. Any suggestions?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

300K is excessively large for most single script files. Have you tried breaking it apart already?

edit: er uhm yeah.
Last edited by feyd on Sun Nov 12, 2006 10:43 pm, edited 1 time in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Includes aren't a big issue, and you'll experience virtually no performance loss in doing so.
User avatar
pgolovko
Forum Commoner
Posts: 38
Joined: Sun Sep 17, 2006 9:13 am

Post by pgolovko »

Thanks guys, I'll break it into smaller includes.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Advantages:
- Modularity

Disadvantages:
- Modularity

I should elaborate... :P

including only the functionality you need will save you both cycles and memory...so modularity in PHP makes sense...

Organziation of code...easier when modular....plus no one likes to look at functions or scripts in the thousands of lines...a few hundred at most per function is ideal...

the minute a function or module starts pushing the 1000 line boundry...I would almost gaurantee it's time to refactor...divide and conquer...and decompose your problems a little more.

A problem which is explained in more than a 1000 lines...is no longer atomic and is likely composed or more than a single "problem" per se...

Modularity has the only side effect of having to work on multiple files concurrently...which some might argue is confusing...but I say...no more confusing than having your mind have to jump back and forth while inside a single large file.

Cheers :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How about:

Code: Select all

if (isset($_GET['type'])) {
    $type = (int) $_GET['type'];
} else {
   $ type = $default_type;
}
$filename = "code{$type}.php";
if (! file_exists($filename) {
    $filename = "code{$default_type}.php";
}
require_once($filename);
That's the first step to a Front Controller.
(#10850)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

Hockey wrote:Advantages:
- Modularity

Disadvantages:
- Modularity

I should elaborate... :P

including only the functionality you need will save you both cycles and memory...so modularity in PHP makes sense...

Organziation of code...easier when modular....plus no one likes to look at functions or scripts in the thousands of lines...a few hundred at most per function is ideal...

the minute a function or module starts pushing the 1000 line boundry...I would almost gaurantee it's time to refactor...divide and conquer...and decompose your problems a little more.

A problem which is explained in more than a 1000 lines...is no longer atomic and is likely composed or more than a single "problem" per se...

Modularity has the only side effect of having to work on multiple files concurrently...which some might argue is confusing...but I say...no more confusing than having your mind have to jump back and forth while inside a single large file.

Cheers :)

wow.... a function pushing 100 rows is a disaster... and a 1000...pfew, nightmare.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Organziation of code...easier when modular....plus no one likes to look at functions or scripts in the thousands of lines...a few hundred at most per function is ideal...
I peg the ideal function/method body length at 20-30 lines excluding comments (me likes comments ;)). The issue is that a long method is long because it is usually handling more than 1 discrete parcel of logic. It's far more readable to break such logic parcels into separate private methods (or other external functions if procedural) with a descriptive name to aid readability.

Back on topic.

A function with thousands of lines is going to be incredibly difficult to maintain. I definitely suggest breaking it up into smaller pieces.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Maugrim_The_Reaper wrote: I peg the ideal function/method body length at 20-30 lines excluding comments (me likes comments ;)). The issue is that a long method is long because it is usually handling more than 1 discrete parcel of logic.
I comment like crazy...so source folding is a really nice feature...but yea I'm around that same count excluding comments... :)
Post Reply