A group of related PHP functions: what is the most appropriate way to organize them?

This may be a dumb question, but I have to ask:

I have a large group of related functions for the project that I am doing. Functions must have access to several global variables, so I thought about putting them into a class and loading the class as needed. I suppose my other option is to simply include them as unrelated functions in the included PHP file, but apparently their meaning is in 1 class. Is this an acceptable practice? I worked with people who did this, but it always seemed like it wasn’t in the spirit of good OOP methods, because classes were almost never created, but functions were still being called. Or maybe I already thought about it.

Any entry would be awesome thanks to the bunch.

+3
source share
3 answers

The class really makes sense. Whenever you can eliminate global variables, that’s good. Whether an instance of the class or a static helper is usually context sensitive. However, for future unit tests, instances allow dependency injection .

+3
source

According to http://en.wikipedia.org/wiki/Class_%28computer_programming%29 , a class defines the constituent elements that allow instances of the class to have state and behavior. If you will only provide behavior (functions) and not state (properties), you must include your functions in the include file and abandon the class overhead.

0
source

, , , , , , , .

Now, if you are going to store these global variables in a class, it is obvious that this is no longer a static class, because the object must have some time to live, and at this point you will need to instantiate the class first, and then call these methods .

Regardless if all the related functions work on the same data, of course, it makes sense to group them in their class.

0
source

All Articles