Herman

Herman

PHP Version Switching

I have been using PHP for development in the company without any obstacles, happily using version 7.4 to write various requirements. Until one day, I started to participate in the development of new features in an old project...

Now I am faced with the situation of developing a new project and adding new features to the old project. The old project uses PHP version 5.6 and has its own xampp environment. If the difference between 5.6 and 7.4 is not significant, it would be fine, but the difference is quite big... Some syntax that I am used to, like simple fn anonymous functions, I often use them, but they are not supported in version 5.6. Even the heredoc syntax is not supported...

These problems should not be discovered just before going live. It is best to have a unified environment with the project.

Until this point, it's not a big problem yet. It sounds like just switching the environment variables. Unfortunately, I use Windows as my development environment in the company, and modifying environment variables in this environment...

Let me put it this way, modifying environment variables on Windows, the timing of when it takes effect is uncontrollable.
In other words, if I need to temporarily switch to a different version of a project and make some changes, if I modify the global environment variables, it won't take effect just by reopening the terminal. I really don't know when the environment variables will start to refresh...

The only way to control when it takes effect is to restart. But many times I don't want to restart! I have scripts running in the background, or I'm doing other things, I don't want to lose progress!

Of course, there is a troublesome way, which is to directly use the path without changing the environment variables. But it's very troublesome, because many configuration files default to using the PHP in the environment. Let's think of a way to solve this problem.

There are mature solutions to this kind of problem.
For example, in the Python community, there is a virtual environment manager that I have been using called miniconda. It can easily control the version of the Python environment, with one virtual environment corresponding to one project.
So I looked it up and found that PHP naturally has a similar manager. But in the end, I didn't use it... because I followed the tutorial a couple of times and failed, and I didn't understand the reason...

I thought about it briefly. If they are all errors that I don't quite understand, then the cost of understanding them is uncertain. In order to use this so-called PHPbrew to manage PHP versions, I would have to reserve at least 2 hours to figure out what's going on...
After thinking about it, forget it, it's not worth it. In order to make the environment variables take effect faster, I have to introduce such a large environment manager, it's better for me to spend 20 minutes to simply implement a script to solve the problem of slow environment variable changes in Windows.

So I wrote a PowerShell script by myself and put it in my profile. I can just use php_version xxx to immediately switch the environment variable to the PHP version I need.

So what magic did I use to make the environment variables take effect immediately?

Ha, the answer may disappoint you. I bypassed the problem.
Since the environment variables take effect slowly and I don't know when they will refresh, it's best not to change the variables casually, just let them stay fixed. So the core problem to solve now is: how to change the actual PHP version being used without changing the path?

This problem is very simple, those who often use Linux should think of it immediately...

That's right, I created a folder called current and created a symbolic link pointing to my default PHP environment in Windows. My Windows environment variables point to this current folder.

What my PowerShell script does is that every time I call this function, it deletes the symbolic link and points it to the new location I specified.

function php_version {
    param (
        [Parameter(Mandatory=$true)]
        [string]$version
    )

    $curr_php_path = "$env:PHP_HOME\current";
    if (Test-Path $curr_php_path) {
        Write-Host "Removing current PHP version"
        cmd /c rmdir $curr_php_path
    }
    
    $target_php_path = "$env:PHP_HOME\$version";
    if (!(Test-Path $target_php_path)) {
        Write-Host "PHP version $version not found"
        return
    }
    
    sudo New-Item -Path $curr_php_path -ItemType SymbolicLink -Value $target_php_path
    Write-Host "PHP version $version set as current"
    php --version
}

That's it, a very simple implementation of PHP version switching~

The inspiration actually comes from the Windows package manager scoop... It manages software versions through symbolic links, and after updating, the new version defaults to pointing to current.

I remember that I only knew about shortcuts on Windows before, and I never thought that there are soft and hard links. When I was naive in college, when studying the operating system course, I thought that symbolic links were just shortcuts, but there are still many differences...

Of course, this method has its limitations, it's different from a true virtual environment like Python. After all, once you make a change, it affects the entire system. Now I have to add php_version php7.4 to the startup configuration to avoid the embarrassment of forgetting to change it back~

By the way, I recommend a plugin for Windows called "Link Shell Extension". After installing it, you can right-click on your files and quickly select the source point, and then create symbolic or hard links to the destination you want. It's much more convenient and intuitive than writing commands directly in the terminal.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.