🔍 HasExtension
Checks whether the specified extension is installed in the current PHP environment.
When to use
Use this for pre-checks before running features that depend on a certain extension, or to assert in CI that required extensions (such as pdo, mbstring) are installed.
Signature
go
func (c *Composer) HasExtension(extension string) (bool, error)Parameters
| Parameter | Type | Description |
|---|---|---|
extension | string | The PHP extension name to check, e.g. mbstring or pdo |
Return value
bool:trueif the extension is installed, otherwisefalseerror: Returns an error when fetching the extension list fails
Example
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatal(err)
}
hasJSON, err := comp.HasExtension("json")
if err != nil {
log.Fatalf("Failed to check extension: %v", err)
}
if hasJSON {
fmt.Println("json extension installed")
} else {
fmt.Println("json extension not installed")
}
}Advanced
- This method internally calls GetExtensions and does an exact match
- To check with a version constraint, use IsPlatformAvailable
- When checking a large batch, call
GetExtensionsonce and iterate yourself to avoid repeatedly executing the command