I have a bunch of small utility PHP functions that I made to solve different scripting problems. Functions like UUID() and trackUserActivity() etc. There are tons of these functions and increasing every day.

Say I have around 50 different small functions and around 100 different scripts for my application. Sometimes those 100 scripts have to call 3 or even 5 of those functions and sometimes they just call one single function out of 50.

I want to know how you organize such a bunch of functions. Do you put all of your functions in a separate folder with each function into a separate file and include individually or create a single class and add all those functions in it and include that class calling functions with object instantiation?

I don't think that it would be good idea to make a class of 50 functions and include that class even for calling a single function. But I wanted to be sure.

I found this question at https://stackoverflow.com/questions/1618895/organize-small-utilities-functions but this is particularly for Java and it does not seems to fit the PHP situation.

  • 2
    you should not expect people downvoting questions to explain why they did so. If you are not happy with that rule here, feel free to ask your question elsewhere, in particular on your own web site. Notice that your question is mostly a matter of opinion – Basile Starynkevitch Apr 18 '20 at 8:42
  • I would put them in a single class and include that class in everything I write. That lets me use any of the functions without having to worry about, or think about, whether or not it might be available. – Dave Apr 18 '20 at 11:56
  • @BasileStarynkevitch Yah I hoped that programmers can be polite too but here I am wrong again. And it's not a matter of opinion to down vote for no reason. SE is a platform for learning and if someone is feeling sick and likes to down vote without commenting the reason, then how come the Questioner know where he went wrong what he should not be doing next time? I Love this platform all come here for the things they don't understand. I hope it makes sense to some otherwise it just a matter of useless discussion – Airy Apr 18 '20 at 15:18
  • I think this question could use more focus. Can you tell us more about why you want to reorganize your code? What problems areas you having? What are your goals for this reorganization? – Greg Burghardt Apr 18 '20 at 16:51
  • 1
    I do agree that things need to be reorganized, though. – Greg Burghardt Apr 18 '20 at 16:52

Both Java and PHP (and C++, and JavaScript) are handling textual source files, and you should use a version control system (e.g. git) to manage them.

The C++11 standard n3337 speaks of translation units (it is the view, by the compiler, of a source file, including preprocessing).

I don't think that it would be good idea to make a class of 50 functions and include that class even for calling a single function. But I wanted to be sure.

This is your opinion. The GCC compiler is coded in C++, and has lots of such classes. It is free software (about 10 to 20 millions lines of source code) and you can study and improve its source code.

The quality of a software project is more related to good development practices (e.g. the famous Joel Test) than to the number of methods in a given class. A given class can have hundred of methods and still be very readable. Look for example inside Qt source code. Read about cyclomatic complexity of call graphs.

The RefPerSys project has lots of classes and some of them have dozen of methods and some of these methods have a hundred of mostly sequential lines.

Some C++ functions of the helpcovid project are quite long but are mostly initializing functions.

I want to know how you expert guys organize your such bunch of functions?

My opinion is that proper documentation (including generated documentation by doxygen) and good, consistent and documented naming or coding conventions is much more important than the number of methods in a given class, or the number of statements implementing a given method.

Recent PHP implementations use techniques similar to compilation to some bytecode, so the size of source code does not matter that much for runtime performance (except for startup time). Time complexity of your code matters a lot more for performance.

BTW you could consider coding your website -if the web server is running Linux- using C (e.g. with libonion and sqlite, or as a plugin to Apache), C++ (e.g. with Wt), or Ocaml (with Ocsigen), or Guile (read first SICP then this) or Haxe. Be also aware of both CGI and FastCGI techniques.

Cybersecurity aspects and ease of maintenance are much more important for public web sites than raw performance. In particular, avoid SQL injections.

My suggestion is for you to publish the source code of your website (this paper explains why). Of course, keep most of its data private (to comply with GDPR regulations), and do backup it quite often.

Christian Queinnec published a lot of academic papers and books related to your concerns. Read about continuations and their relation to the WWW (mostly but not only HTTP, also SMTP, IMAP, DNS, WebSockets, Web services, AJAX, SOAP, JSONRPC, etc...).

  • Thanks. If I am not wrong, do you mean to say that It would be a good idea to manage all of my those functions through a single class? Even though if I have to call a single function including such big file of 50 functions won't impact on performance? – Airy Apr 18 '20 at 8:13
  • Don't worry about performance first. Put your efforts on program correctness and documentation. Code first, optimize later. – Basile Starynkevitch Apr 18 '20 at 8:14
  • Thanks Basile. You have provided good links for better understanding. Please allow me some time so I can go through these. – Airy Apr 18 '20 at 8:18
  • If either RefPerSys or HelpCovid interests you, please send me an email to basile@starynkevitch.net – Basile Starynkevitch Apr 18 '20 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.