Can the json
extension be disabled in PHP? The doc says that:
As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.
But some people in the comments say that the json extension is sometimes provided as a separate package.
Can the json
extension be explicitly disabled, or can we be confident that it's always available?
Background: I want to make a class in a library of mine implement JsonSerializable
, but that may be a BC break if the interface is not always declared, and the library suddenly relies on an extension that's not always available.
No, the JSON extension cannot be disabled anymore.
Yes, any PHP extension can be installed, uninstalled, enabled, or disabled at will.
The json
extension - despite its ubiquity - is still just an extension and can be removed in this way as well.
There are a couple of cases in which the json
extension might not exist:
The administrator disabled/uninstalled it:
;extension=json
The version of PHP that was installed was compiled from source manually, and the json
extension was left out:
--disable-json
The extension is bundled as a separate package; for example, on Fedora you have to install the php-json
package explicitly.
The important part of your question: Can we be confident that it's always available
Normally, I would say no. However unlikely the case is that this particular extension is disabled or left out, it still doesn't mean that it won't happen.
If your intended audience is limited to people who probably wouldn't touch those kinds of settings, then you might be safe, but there's no guarantee.
My suggestion: Build your library as a Composer package, and declare ext-json
as a dependency. That way, you can provide installation instructions as a Composer package and if the underlying system doesn't meet your package requirements, the installation will fail and the user will be alerted to the missing extension.