extension_loaded / get_loaded_extensions
extension_loaded(string $extension): bool · get_loaded_extensions(bool $zend_extensions = false): array
Extension introspection: extension_loaded probes one extension by name (compiled in or shared-loaded), get_loaded_extensions lists every loaded extension; mirrors hash_algos' inventory-consumer pattern — probe before use is step one of portable PHP.
C C23
/* no runtime extension registry: C links statically; dlopen probes shared
objects on demand - and library sonames are platform artifacts */
#include <dlfcn.h>
#include <stdio.h>
int main(void) {
void *libm = dlopen("libm.dylib", RTLD_LAZY); /* macOS: .dylib; Linux: libm.so.6 */
printf("%s\n", libm ? "loaded" : "absent");
void *ghost = dlopen("no_such_lib_42.so", RTLD_LAZY);
printf("%s\n", ghost ? "loaded" : "absent");
if (libm) {
dlclose(libm);
}
return 0;
}
loaded absent
C++ C++26
#include <dlfcn.h>
#include <iostream>
// same as C: static linking, dlopen on demand, platform-specific sonames
int main() {
void *libm = dlopen("libm.dylib", RTLD_LAZY); /* macOS: .dylib; Linux: libm.so.6 */
std::cout << (libm ? "loaded" : "absent") << "\n";
void *ghost = dlopen("no_such_lib_42.so", RTLD_LAZY);
std::cout << (ghost ? "loaded" : "absent") << "\n";
if (libm) {
dlclose(libm);
}
return 0;
}
loaded absent
C# v11.0.100
using System;
using System.Linq;
using System.Reflection;
// loaded assemblies ARE the inventory; Type.GetType probes the type surface
Console.WriteLine(AppDomain.CurrentDomain.GetAssemblies().Length >= 1); // inventory non-empty
Console.WriteLine(AppDomain.CurrentDomain.GetAssemblies().Any(a => a.GetName().Name == "System.Private.CoreLib"));
Console.WriteLine(Type.GetType("System.String") != null); // core type present
Console.WriteLine(Type.GetType("No.Such.Type42") == null); // probe misses cleanly
True True True True
Go v1.27.4
package main
import (
"fmt"
"strconv"
)
// Go binaries link everything statically - "is this extension loaded" is a
// compile-time fact, not a runtime list (the plugin package is Linux-only)
func main() {
_, err := strconv.Atoi("42")
fmt.Println(err == nil) // if it imported, it is linked - always usable
fmt.Println(strconv.Itoa(-1) == "-1")
}
true true
Java v27.0.1
public class Main {
public static void main(String[] args) {
// Class.forName: runtime probe for a type - the extension_loaded analog
try {
Class<?> c = Class.forName("java.util.zip.Deflater");
System.out.println(c != null); // core "extension" present
} catch (ClassNotFoundException e) {
System.out.println(false);
}
try {
Class.forName("no.such.Class42");
System.out.println("loaded");
} catch (ClassNotFoundException e) {
System.out.println("absent");
}
System.out.println(ModuleLayer.boot().modules().size() > 20); // the inventory
}
}
true absent true
JavaScript v26.13.1
// require.resolve probes the module surface; process.versions is the inventory
console.log(require.resolve("fs").length > 0); // core module resolvable
try {
require.resolve("no_such_module_42");
console.log("loaded");
} catch {
console.log("absent");
}
console.log(Object.keys(process.versions).length > 3); // node/bundled deps inventory
true absent true
PHP v8.6.0
// extension_loaded(name): is that extension loaded right now?
// get_loaded_extensions(): the full inventory (hash_algos is an inventory
// consumer - probe-before-use is the portable-PHP pattern)
var_dump(extension_loaded("mbstring"));
var_dump(extension_loaded("calendar"));
var_dump(extension_loaded("no_such_ext_42"));
$exts = get_loaded_extensions();
echo in_array("Core", $exts, true) && in_array("date", $exts, true) && in_array("json", $exts, true) && in_array("pcre", $exts, true) ? "core extensions present\n" : "missing core\n";
echo count($exts) > 20 ? "inventory plausible\n" : "inventory too small\n"; // derived fact: a real runtime loads dozens
bool(true) bool(true) bool(false) core extensions present inventory plausible
Python v3.15.2
import importlib.util
import sys
# find_spec answers "is it importable": Python's closest extension_loaded analog
print(importlib.util.find_spec("json") is not None)
print(importlib.util.find_spec("no_such_module_42") is None)
# sys.stdlib_module_names (3.10+): the inventory - get_loaded_extensions analog
mods = sys.stdlib_module_names
print(len(mods) > 100) # derived fact: the std inventory is large
print("json" in mods and "calendar" in mods)
True True True True
Rust v1.99.1
// no dynamic extension system: rustc links statically, so "loaded" is a
// link-time fact; honest demo: probe a hand-rolled registry
fn main() {
let registry = ["std", "core", "alloc"]; // the always-linked trio
println!("{}", registry.contains(&"std"));
println!("{}", !registry.contains(&"no_such_ext_42"));
println!("{}", registry.len() > 2); // derived fact scaled to the demo registry
}
true true true
TypeScript v26.13.1
// require.resolve probes the module surface; process.versions is the inventory
const resolvable: boolean = require.resolve("fs").length > 0;
console.log(resolvable);
try {
require.resolve("no_such_module_42");
console.log("loaded");
} catch {
console.log("absent");
}
console.log(Object.keys(process.versions).length > 3);
true absent true
Language differences
Probe vs inventory: extension_loaded probes by name (answers "can I use it"), get_loaded_extensions shows the panorama (answers "what does this environment look like") — mirroring hash_algos' inventory-consumer pattern; the deterministic assertions: mbstring and calendar are loaded (fixed facts of this site's validation environment), a fictional extension name is always false, the inventory contains the Core/date/json/pcre core extensions and exceeds 20 entries (derived fact).
PHP: extension_loaded("mbstring") is case-insensitive and covers both compiled-in and dl-loaded extensions; get_loaded_extensions(true) lists Zend extensions only (opcache etc.); related links: phpversion("ext") fetches an extension's version, ini_get reads its registered directives.
Python: importlib.util.find_spec(name) probes the import surface (answers even before import); sys.stdlib_module_names (3.10+) is a static stdlib inventory — note it lists importable std names, not loaded C extensions, unlike PHP's loaded semantics (Python's C extensions load on import, no separate registry).
Go: binaries link statically, so "is it available" is decided at compile time; runtime probing reduces to the plugin package (Linux-only) or handwritten dlopen (cgo); no inventory exists — go version -m <binary> reads the build dependency list offline.
Java: Class.forName is the runtime type probe (ClassNotFoundException means "not installed"); under JPMS, ModuleLayer.boot().modules() is the inventory; the extension_loaded analog is "it is on the classpath".
C#: AppDomain.CurrentDomain.GetAssemblies() is the loaded-assembly inventory (lazy — it grows on demand); Type.GetType probes the type surface; assembly probing is closest to PHP's loaded semantics.
JavaScript/TypeScript: require.resolve(name) answers "does it resolve" without executing the module (wrap in try/catch); process.versions is the version inventory of the runtime's own components (a weak get_loaded_extensions analog).
C/C++: no runtime registry — static linking decides everything at compile time; dlopen(3) (dlfcn.h) probes shared objects on demand, but sonames are platform artifacts (macOS libm.dylib / Linux libm.so.6): the example hardcodes the macOS name, Linux ports replace it and add -ldl at link time; call dlerror() after failure for the reason.
Rust: no dynamic extension system — crates link at compile time, so "probing" means "can I reference it"; dynamic loading uses the libloading crate (a dlopen wrapper); the example follows the honest strategy with a hand-rolled registry carrying the same assertions.