PHP Helper
Some code must choose a path based on the PHP runtime itself: whether it runs under a web server or the command line, whether an extension is available, or how errors are reported. PhpHelper answers these questions without reading PHP_SAPI or extension_loaded() at every call site.
Most methods are static. getVersion() and hasXdebug() are instance methods, so create an instance first:
use Windwalker\Environment\PhpHelper;
$php = new PhpHelper();Assembly
PhpHelper has no configuration and no constructor arguments. Call its static methods directly, or create an instance for the two instance methods.
Showcase
The following class collects the runtime capabilities that a service needs to report, for example on a diagnostics page:
use Windwalker\Environment\PhpHelper;
final class RuntimeCapabilities
{
public function describe(): array
{
$php = new PhpHelper();
return [
'sapi' => PHP_SAPI,
'version' => $php->getVersion(),
'web' => PhpHelper::isWebServer(),
'cli' => PhpHelper::isCli(),
'xdebug' => $php->hasXdebug(),
'openssl' => PhpHelper::hasOpenssl(),
'curl' => PhpHelper::hasCurl(),
];
}
}SAPI Detection
isCli()is true for thecliandcli-serverSAPIs. The built-in development server counts as CLI.isWebServer()is true for theapache,cgi,fast-cgi, andsrvSAPIs.isEmbed()is true for theembedSAPI.isPHP()is true unless the runtime is HHVM, whichisHHVM()detects through theHHVM_VERSIONconstant.
Extension Checks
hasOpenssl(),hasCurl(),hasPcntl(), andhasMcrypt()check whether the matching extension is available.hasXdebug()is an instance method. It returns true only when the runtime is PHP and the Xdebug extension is loaded.
Check the extension before you call a feature that needs it. Do not assume that a common extension is always present in a container or on a shared host.
Error Reporting
setStrict()callserror_reporting(-1), which reports all errors.setMuted()callserror_reporting(0), which reports none.
Both change the setting for the whole process. Save the previous value with error_reporting() and restore it when the change is finished.
Other Helpers
getVersion() returns PHP_VERSION, which is the version of the running binary. It does not return the language level configured for a project.
Pitfalls
WARNING
PHP-FPM is not a web SAPI here. Under PHP-FPM, PHP_SAPI is fpm-fcgi, which isWebServer() does not list. Use PHP_SAPI or your own check when the code must detect PHP-FPM.
WARNING
Muting hides everything. setMuted() also hides deprecation and warning messages. Use it only around a narrow block of code, and restore the previous level afterwards.
Tip: Removed extensions. The mcrypt extension was removed from PHP 8.0, so hasMcrypt() is false on current runtimes unless a legacy build is used.