I have a project which is entirely functional code, as in: all functions are static and should not depend upon how they were called. If you call function foo("hello", 12.345, true);
it should not matter at all the context around that function call -- it should always perform the same action.
The problem I have is that I'm using two third-party libraries, who, through composure, both import GuzzleHttp and define it as a Namespace in their autoloaders, though they use different versions of the Guzzle client. (The two 3rd party SDKs I've imported are for Plivo and libphonenumber, though not sure it's relevant.) The way the functions are written I do something like this:
function sendPlivoSms($number, $message) {
require_once("/vendor/plivo/autoloader.php");
$gz_client = new \GuzzleHttp\Client();
// more code here
}
function formatNationalPhoneNumber($number) {
require_once("/vendor/libphonenumber/autoloader.php");
$gz_client = new \GuzzleHttp\Client();
// more code here
}
The issue arises when I call formatNationalPhoneNumber
before sendPlivoSms
-- it loads the GuzzleHttp Namespace, which then causes the sending SMS function to call the wrong Client class, and therefore breaks (specifically due to a missing chooseHandler
function). The autoloaders are loading different versions of the Guzzle code, at different locations in the code source (which is 100% okay aside from the namespace hijacking).
Is there a way for me to isolate these function calls so that do not steal each other's namespace references?
For example, could I "unset" the namespace at the end of the function call (again: to keep the function perfectly static)? Or is there a way for me to put the namespace under a limited scope somehow?
File tree and composer.json follows:
/
composer.json
tons of other directories and code here...
./thirdparty/
./libphonenumber/
./vendor/
./composer.json
./vendor/
all composer directories loaded here, except the libphonenumber ones