PHP Classes

Multiple PHP Versions Windows: Switch the version of PHP that will run on Windows

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 77 All time: 10,129 This week: 571Up
Version License PHP version Categories
multiphp 1.0.0MIT/X Consortium ...5PHP 5, Console, Windows
Description 

Author

This package can switch the version of PHP that will run on Windows.

It can take as a parameter the version of PHP that you wish to use to run PHP scripts when they execute from the command line.

The class can write a batch script named PHP.BAT that executes the specified version of the PHP CLI executable program passing to PHP any parameters to that batch script.

Innovation Award
PHP Programming Innovation award nominee
December 2021
Number 7
When you develop code for other developers, it is always good to test that code on different versions of PHP, so the code is ready to handle the differences between the different versions of PHP.

This package creates a batch script for Windows named PHP.BAT that can be changed to run different versions of the PHP CLI version installed in the current computer.

This way, it is easy to switch the version of PHP that testing code will use.

Manuel Lemos
Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 20 packages by
Country: Turkey Turkey
Age: 21
All time rank: 276644 in Turkey Turkey
Week rank: 52 Up2 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 13x

Example

#!/usr/bin/env php
<?php

if (php_sapi_name() !== 'cli') {
    exit;
}

require
__DIR__ . '/vendor/autoload.php';

use
Minicli\App;
use
Minicli\Command\CommandCall;
use
Minicli\Exception\CommandNotFoundException;
use
Minicli\Input;

$app = new App([
   
'app_path' => __DIR__ . '/app/Command',
   
'theme' => '\Unicorn',
   
'debug' => false,
]);

$configFile = __DIR__ . "\\config.json";

if (!
file_exists($configFile)) {
   
$app->getPrinter()->display("Welcome to MultiPHP.");

   
$input = new Input("Your environment base directory: ");
   
$baseDirectory = $input->read();

   
$input = new Input("Your binary directory: ");
   
$binaryDirectory = $input->read();

   
file_put_contents($configFile, json_encode([
       
"base_directory" => $baseDirectory,
       
"binary_directory" => $binaryDirectory,
    ]));

   
$app->getPrinter()->success("Configuration saved. Please rerun script.", true);
    return;
}

$config = json_decode(file_get_contents($configFile));

$baseDirectory = $config->base_directory . "\\";
$binaryDirectory = $config->binary_directory . "\\";

$supportedVersions = [
   
"7.3",
   
"7.4",
   
"8.0",
   
"8.1",
];

$app->registerCommand("change", function (CommandCall $test) use ($app, $baseDirectory, $binaryDirectory, $supportedVersions) {
   
$version = $test->getParam("version");
    if (
strlen($version) < 1) {
       
$app->getPrinter()->error("The argument 'version' is required!");
        return;
    }

    if (!
in_array($version, $supportedVersions)) {
       
$app->getPrinter()->error("The " . $version . " is unsupported!" . PHP_EOL ."Supported versions: " . implode(", ", $supportedVersions) . PHP_EOL, false);
        return;
    }

   
$path = $baseDirectory . $version . "\\php.exe";

    if (!
file_exists($path)) {
       
$app->getPrinter()->error("No executable php found in: " . $path . PHP_EOL);
        return;
    }

    if (!
is_dir($binaryDirectory)) {
       
mkdir($binaryDirectory);
    }

   
file_put_contents($binaryDirectory . "\\php.bat", "@echo OFF" . PHP_EOL . $path . " %*");
   
$app->getPrinter()->success("Environment is changed successfully.", true);
});

try {
   
$app->runCommand($argv);
} catch (
CommandNotFoundException $notFoundException) {
   
$app->getPrinter()->error("Command Not Found.");
    return
1;
} catch (
Exception $exception) {
    if (
$app->config->debug) {
       
$app->getPrinter()->error("An error occurred:");
       
$app->getPrinter()->error($exception->getMessage());
    }
    return
1;
}

return
0;


Details

MultiPHP

Installation

  • Clone this repo: ```git clone https://github.com/isaeken/multiphp.git```
  • Move repo to secure folder example: ``C:\dev\multiphp``
  • Install dependencies: ```composer install --no-dev --optimize-autoloader```
  • Add path to environment variables
  • Run ```multiphp``` command in your terminal and enter binary directory & environment directory - Example binary directory: ```C:\dev\bin``` - Example environment directory: ```C:\dev\php```
  • Download & configure php versions (7.3, 7.4, 8.0, 8.1) to environment directory - Example: ```C:\dev\php\8.1\php.exe```

Change PHP environment

Open terminal and run

multiphp change version=8.1

  Files folder image Files  
File Role Description
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE Lic. License text
Plain text file multiphp Example Example script
Plain text file multiphp.bat Data Auxiliary data
Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:77
This week:0
All time:10,129
This week:571Up