Jump to content

[SOLVED] Best structure for a function library?


pocobueno1388

Recommended Posts

I was wondering what the best structure was to use for a function library. Say I had a bunch of functions that I used on  scripts every once in a while, would you guys suggest putting each function in its own individual file in a function folder and just included it whenever I wanted to use it, or would you suggest saving all the functions one after another as one big script?

 

The problem I see with putting them all in one file would be when I included that file and only wanted to use one of the functions it would include all the functions I didn't need. Would this take up more space for the page I include it on, or would it only take up space/speed if I used the function?

 

I'm not really in this situation, but I am just curious what the best way of doing it would be for future reference. Thanks =]

 

 

Link to comment
Share on other sites

Yes...but when you include the file in your script, do you take the entire file and include it even if you are just wanting a single function?

 

Maybe what you are trying to say is when you need a function you just copy and paste it into your script from that text file? That would be okay to do...but when you use the function in quite a few scripts it would be better to just be able to include it easily.

Link to comment
Share on other sites

you just copy and paste it into your script from that text file?

Horrible idea; don't do that.

 

Organize the functions into logical units and then expose them through "namespaces;" I've quoted namespaces as PHP doesn't actually support them, but you can fake the behavior with classes.

 

For example, let's say you had the following functions:

 

<?php
a(); // regexp function
b(); // regexp function
c(); // html helper function
d(); // sanitizing function
e(); // html helper
// and so on
?>

 

You basically have three categories of functions there: regexp, html helper, & sanitizing.

 

For each category, you create a separate file where the filename starts with "N" followed by the category:

NRegExp.php, NHTML.php, NSanitizer.php

 

NRegExp.php

<?php
class NRegExp{
  function a(){
    // Function a body
  }

  function b(){
    // Function b body
  }
}
?>

 

I recommend storing each of these files within a "namespaces" directory and declaring a constant:

define("DIR_NAMESPACE", "/path/to/dir");

 

Now, to access your regexp library in a script:

<?php
require_once(DIR_NAMESPACE . "/NRegExp.php");

// Some php
NRegExp::a(); // Call func a
// Some more php
?>

 

I see it time and time again where people creating scripts are concerned about the number of files, or the size of included files, within your scripts.  You should not concern yourself with this.  You have a greater potential to bring your site to it's knees by having a bad database setup or bad algorithms within your code than including a bunch of files.

Link to comment
Share on other sites

Thank you =] That clears up a lot. I was thinking classes would probably be the better way to go, but thought the idea would better be understood if I said it in terms of functions.

 

Well, thanks again. I am sure I will be using this technique sometime or another.

Link to comment
Share on other sites

personally... i make a functions folder... with an index, which glob()s and require_once()'s all the files in that folder, and inside, i have each of my functions seperated(1 per file(for the most part, i think in have one file with 4... but there all dependant on eachother))...

 

for example...

get_url.php

<?
function get_url(){
if(!empty($_SERVER['QUERY_STRING'])) return 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
else return 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
}
?>

thats it.

 

the one with 4 files, is a navigation script i made, allowing you to add/remove topics, and groups, and output... 4 parts of the same program...

 

but thats just me... makes debuging a breeze!... dont have to count lines of code, looking to find that you forgot a {...

Link to comment
Share on other sites

I added a new feature to the product we sell recently; after programming for two weeks I was curious as to just how much code I'd written.  I wrote a short script that opens up all of the source files I had added to our project in adding this feature; as each file is opened it's contents are concatenated to a string variable and the final contents of that var are dumped into another file.

 

To give you an idea of how fast a web server processes files, I added a time check at the beginning and end of that script.

 

Time required to open 30 files, concatenate their contents, and dump the 4600 lines of code back into a dump file:

 

0.63623100 1179511579 (microtime - start)

0.64138600 1179511579 (microtime - end)

 

Total time required: ~5 one thousandths of a second

 

Granted, this doesn't add the complexity of parsing the files' contents as PHP, but the bottom line is if you have optimization concerns, worry about your DB and algorithms before you worry about the size of your source code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.