Breaking up logic: "include" files or functions

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
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Breaking up logic: "include" files or functions

Post by pickle »

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.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

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

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

Post by fractalvibes »

If you are using that chunk of code in multiple places, then why not in a function that is in an include file? Could be an ordinary function or a method of an object that you include. Either way, if it need be changed, change in 1 file only...

fv
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

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.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

mcgruff, you crazy java thinker guy ;)

have you lost your procedural roots? lol
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'm on a mission these days. Can't cope with debugging spaghetti code which, with a bit of organisation, probably wouldn't have half as many bugs.

We badly need some refactoring tutorials. I might even get around to writing something. Sigh. If only I had just one spare afternoon..
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

JPlush76 wrote:have you lost your procedural roots? lol
We can only hope so

What fun programming would be if everyone lost their procedural roots
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

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....
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

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

Passing parameters around to functions can start to get awkward though - that's when it's time to look at classes.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post by Straterra »

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?
ilovetoast
Forum Contributor
Posts: 142
Joined: Thu Jan 15, 2004 7:34 pm

Post by ilovetoast »

Echo needs a class.

sugar on toast
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

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

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;
}

?>
One doesn't just write code that a computer can understand: you're also writing code that (hopefully) people can understand. Functions provide "chapters and titles" which make a script easier to read at a glance. With chunks of code encapsulated in well-named functions, code can almost read like pure english - and well-factored code is easier to debug & maintain.

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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

ilovetoast wrote:Echo needs a class.

sugar on toast
Lol.

What I can't figure out is if sugar is a child of toast or if toast should extend a sugar class. And how to avoid beverage-dependency in order to support multiple presentation formats: coffee, tea, hot milk...?
ilovetoast
Forum Contributor
Posts: 142
Joined: Thu Jan 15, 2004 7:34 pm

Post by ilovetoast »

LMAO.

But seriously, sugar is a interface. Beverage issue remains under construction.

As to the more pertinant, albeit lesser, question of include files, etc., I strongly agree with McGruff's well explained points. Anything more from me would be repetitive.

butter the toast
Post Reply