The coupling created by accessing all of the PHP global functions hampers the testability of PHP code. This happens often because a large number of useful extensions are accessed only through global functions. In his latest post, Mike Naberezny demonstrates how you can get over this by using an LDAP server and ExtensionProxy. Maike takes the example of the following code snippet:
= ldap_connect(, );
if (! ) {
// error logging
return false;
}
In this snippet there are two code paths showing the connection succeeding, and it failing. Both are difficult to test because of the coupling to the global function ldap_connect() provided by the LDAP extension. To make it succeed you can use an LDAP server, using the extension through an object instead of calling the extension function directly. Mike says that writing these wrappers and maintaining them can be a pain and this is often the rationale given for not using them. His easy solution is to use the ExtensionProxy class.
Since most PHP extensions prefix all of their functions with the name followed by an underscore, it’s easy to wrap them with something like the class above. The connection sample snippet (shown earlier) is turned into an easily testable version, like this:
= new ExtensionProxy('ldap');
...
= ->connect(, );
if (! ) {
// error logging
return false;
}




