🐘 ExecPHP
Runs a vendor binary using the specified PHP binary. Equivalent to running composer exec --php=<php> <binary> -- <args...>.
When to use
Use it when the project has multiple PHP versions and you need to explicitly run a vendor bin with a specific PHP interpreter (for example, to test compatibility across PHP versions).
Signature
go
func (c *Composer) ExecPHP(php string, binary string, args ...string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
php | string | Path to the PHP executable, for example /usr/bin/php8.2 |
binary | string | Vendor binary file name, for example phpunit |
args | ...string | Argument list forwarded to the binary |
Return value
| Return value | Type | Description |
|---|---|---|
| First return value | string | The stdout output of the binary execution |
| Second return value | error | Returned when execution 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.Fatalf("initialization failed: %v", err)
}
output, err := comp.ExecPHP("/usr/bin/php8.2", "phpunit", "--version")
if err != nil {
log.Fatalf("execution failed: %v", err)
}
fmt.Println(output)
}Advanced
- When you don't need to specify PHP, use the simpler Exec.
- To list available binaries, use ExecWithList.
- To execute in a specific working directory, refer to
ExecWithWorkingDir, used together with Exec.