Breaking up logic: "include" files or functions
Moderator: General Moderators
Breaking up logic: "include" files or functions
Hi all,
Just a question I want to pose to the community. When you are splitting up logic, say for example, an array transfer sub-routine, do you commonly put it in an include file and then include that file, or do you put the logic in a function then call the function?
What are the pro's and con's of doing it either way?
I've recently started doing a number of small webapps and I've experimented with both methods, but I can't seem to decide which is better.
Thanks everyone.
Just a question I want to pose to the community. When you are splitting up logic, say for example, an array transfer sub-routine, do you commonly put it in an include file and then include that file, or do you put the logic in a function then call the function?
What are the pro's and con's of doing it either way?
I've recently started doing a number of small webapps and I've experimented with both methods, but I can't seem to decide which is better.
Thanks everyone.
depends, i only use functions when i know i will be useing it in few pages, if it is something small, like a array check or something then i just put it in a file and include, overall, its up to you i guess.....kinda makes hard to choose i guess.
what i uselly do is to have a file called "functions.php" and i put all my site/app functions in there, so when i need it i can include it, now days i just make a class for the whole process, a image gallery for example
.
having functions in one place means you dont need to look in all of your files to change something, so functions are my first choice.
what i uselly do is to have a file called "functions.php" and i put all my site/app functions in there, so when i need it i can include it, now days i just make a class for the whole process, a image gallery for example
having functions in one place means you dont need to look in all of your files to change something, so functions are my first choice.
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
Always put everything in functions (and then put all your functions in classes).
Each function should carry out just one task. Each class should similarly do one "thing".
There really shouldn't be any "bare code" anywhere (as always there are exceptions to the rule but precious few). Include a file with function defs instead.
Functions aren't created only when they can be re-used. Think of them more as ways to organise your code with chapters & titles.
Each function should carry out just one task. Each class should similarly do one "thing".
There really shouldn't be any "bare code" anywhere (as always there are exceptions to the rule but precious few). Include a file with function defs instead.
Functions aren't created only when they can be re-used. Think of them more as ways to organise your code with chapters & titles.
-
Straterra
- Forum Regular
- Posts: 527
- Joined: Mon Nov 24, 2003 8:46 am
- Location: Indianapolis, Indiana
- Contact:
A function...for everything? I think that would become even more combersome than spaghetti code. Just think..all 9000 users change all of their code into functions and classes...Then, we try to help them..All they show us is the line calling the function...Then, we ask for the function, then that function calls some variable from inside the class...Then, we use 10 posts just for someone to post their entire class, to just find out that they misspelled mysql....
The McGruff method is something I use all of the time now, even with small PHP based sites which don't do much. People will only grasp why using classes() and functions() is a good thing to do once they have tried it... and understand it.
The only time I use include files within functions is when I need to load a 'settings.php' page or something which contains a load of variables and/or arrays etc, or when I need to pull a chunk of HTML code into a function for some reason.
The only time I use include files within functions is when I need to load a 'settings.php' page or something which contains a load of variables and/or arrays etc, or when I need to pull a chunk of HTML code into a function for some reason.
Not at all. Suppose they did give us the wrong function: it doesn't take a second to pass some test parameters to verify it's OK then move on to the next possible source of the problem. Single-shot functions, half a dozen lines or so, are easy to test - often there wouldn't be a question in the first place.Straterra wrote:A function...for everything? I think that would become even more combersome than spaghetti code. Just think..all 9000 users change all of their code into functions and classes...Then, we try to help them..All they show us is the line calling the function...Then, we ask for the function, then that function calls some variable from inside the class...Then, we use 10 posts just for someone to post their entire class, to just find out that they misspelled mysql....
Passing parameters around to functions can start to get awkward though - that's when it's time to look at classes.
-
ilovetoast
- Forum Contributor
- Posts: 142
- Joined: Thu Jan 15, 2004 7:34 pm
Re-writing native fns can be useful. I often use this to log queries in a $monitor object. __FILE__ and __LINE__ are passed in via $location.Straterra wrote:My point is..its retarted to have functions for functions that are already in PHP that only require 1 argument..Like echo, or crap like that. Don't you agree?
Code: Select all
<?php
function sqlQuery($sql, &$monitor, $location)
{
$monitor->setArray('sql', $location . ' <br /> ' . $sql);
$query = mysql_query($sql) or die('<b>sql error:</b> ' . mysql_errno() . ' - '
. mysql_error() . '<br/>' . $location .
'<br />'
);
return $query;
}
?>Deciding what fns to build gets you started thinking in terms of responsibilities. That's a vital skill for creating good program designs.
Last edited by McGruff on Tue Aug 09, 2005 9:47 pm, edited 1 time in total.
-
ilovetoast
- Forum Contributor
- Posts: 142
- Joined: Thu Jan 15, 2004 7:34 pm