Jump to content

Using an include to access user functions


henryhenry

Recommended Posts

I am trying to make my code a bit 'cleaner' and I am making useful functions out of things and then putting them in a nicely organised seperate 'function.php' file which I include on every page.I am a bit concerned that this may slow things down though especially as I'm using SQL in the functions. Right now it's not noticeable on my local machine (using localhost - apache). I have no real experience so I'm not sure if I'm going to regret this functions.php later? Is it better to put functions on the page itself or store in an include file? And is it better to use functions or to have the 'plain' code sitting in the page...Anyone have any guidance/experience - what do you do with functions?

Link to comment
Share on other sites

Hi!When i have functions that I use, or may want to use, in different files I prefer to put them in a seperate file (such as functions.php), as it gets a lot easier to reuse them...To use functions to seperate for example different parts of the layout and/or data collection is a good way to get cleaner, and easier maintained, code. But be aware to not use too many functions as it then can be a nightmare to maintain...The use of functions shouldn't slow down the execution (as it's just another block of code, much like an if-statement), but what might slow things down is if you open and close the connection to the database many times (like once in every function), it's simpler and better to open the connection in the beginning of the file/script, use it in the functions and then close it...Hope that helped.Good Luck and Don't Panic!

Link to comment
Share on other sites

It doesn't matter if you have functions in an external file vs. inline in the code, or whatever. You do pay a small performance penalty for a function call, it is not the same thing as an if statement or any other control statement. A function call causes a context switch to occur. The PHP interpreter that runs the code keeps track of something called a stack. The stack is a list of what are referred to as program contexts. The context contains things like locally defined variables, locally defined functions, etc. When a function call takes place, execution briefly pauses while the current context is saved, and then a new context is pushed onto the stack. And then execution resumes at the beginning of the function, and the function has its own context to use. Every time a variable is referenced it is looked up in the current context. When the function ends, the context for that function is popped off the stack, the previously saved context is back on the top of the stack, and execution resumes after the function call.So, there is a small performance penalty for using functions, but it is no reason not to use functions in general. Just don't have something like this:

function a(){  return b();}function b(){  return c();}function c(){  return d();}function d(){  return e();}function e(){  return 1;}

That's just a simple example, but you can see that calling the function a() would cause 5 different context switches to take place, where 5 contexts would be pushed onto the stack, and popped right off again. That is much less efficient then if you just called the last function directly and skipped the aliases. For that reason, whenever you see a function on php.net that it says is an alias, like this one:http://www.php.net/manual/en/function.join.phpAlways use the parent function. For example, always use implode instead of join.

Link to comment
Share on other sites

Well, you learn something new every day... :?)The difference isn't that big in time any way. :?)I haven't read so much about the "internals "in PHP, used my own script-interpreter as "reference"..Thanks guy for the info.

Link to comment
Share on other sites

The difference isn't that big in time any way. :?)
No, it isn't. Just one more thing to keep in mind. For extremely high-performance code, there are various techniques you can use to speed code up, and elminating function calls in things like loops is one of them. For any other code, you're probably fine.
Link to comment
Share on other sites

The only worry is really about high-performance code. If your code isn't going to be under very high levels of stress for long periods of time, then function calls are no problem at all. If the code is high-performance though, saving a few milliseconds here and there will add up over the long run. But if your site gets 1000 hits a day, saving a few milliseconds would never be noticeable.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...