pocobueno1388 Posted May 18, 2007 Share Posted May 18, 2007 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 =] Quote Link to comment https://forums.phpfreaks.com/topic/52029-solved-best-structure-for-a-function-library/ Share on other sites More sharing options...
chigley Posted May 18, 2007 Share Posted May 18, 2007 I just save mine in a .txt file on my desktop computer Quote Link to comment https://forums.phpfreaks.com/topic/52029-solved-best-structure-for-a-function-library/#findComment-256444 Share on other sites More sharing options...
pocobueno1388 Posted May 18, 2007 Author Share Posted May 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52029-solved-best-structure-for-a-function-library/#findComment-256448 Share on other sites More sharing options...
roopurt18 Posted May 18, 2007 Share Posted May 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52029-solved-best-structure-for-a-function-library/#findComment-256460 Share on other sites More sharing options...
pocobueno1388 Posted May 18, 2007 Author Share Posted May 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52029-solved-best-structure-for-a-function-library/#findComment-256470 Share on other sites More sharing options...
taith Posted May 18, 2007 Share Posted May 18, 2007 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 {... Quote Link to comment https://forums.phpfreaks.com/topic/52029-solved-best-structure-for-a-function-library/#findComment-256475 Share on other sites More sharing options...
roopurt18 Posted May 18, 2007 Share Posted May 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52029-solved-best-structure-for-a-function-library/#findComment-256480 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.