I have experience programming in Perl, Python and Javascript, but recently inherited a PHP project that looks like a lot of spaghetti code to me.
I am trying to refactor the project in order to get it properly under unit test. For me, this means looking through the source code and breaking everything up into small functions that don't use global variables and each perform one specific task.
I understand that PHP's module system works a bit like Javascript: all of the source files are concatenated into one upon include
.
This means that functions in two separate files cannot have the same name, assuming one of the files is going to use the other.
My question is: how should I be defining my functions to deal with this global scope issue in PHP?
In Javascript, I would use the module pattern like so:
var MyModule = (function () {
return {
'func1': function () {},
'func2': function () {}
};
}());
Or some variation of that. Is there a way to do this in PHP? Also, what are the proper conventions for this?