Skip to content

🐘 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 ​

ParameterTypeDescription
phpstringPath to the PHP executable, for example /usr/bin/php8.2
binarystringVendor binary file name, for example phpunit
args...stringArgument list forwarded to the binary

Return value ​

Return valueTypeDescription
First return valuestringThe stdout output of the binary execution
Second return valueerrorReturned 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.

Released under the MIT License