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 join an old project to participate in the development of new features...

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, such as the simple fn anonymous function, I often use, but it is not supported in version 5.6. Even the long string tag is not supported...

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

Up to this point, it is 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 is uncontrollable in terms of when it takes effect.
In other words, if I need to temporarily switch to a different version of the project to make some changes, and 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 the effect is to restart. But many times I don't want to restart! I have scripts running in the background or doing other things, and I don't want to lose progress!

Of course, there is a troublesome way, which is not to change the environment variables directly and use the path instead. But it is very troublesome because many configuration files default to using 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 once or twice and failed, and I didn't understand the reason...

I thought about it briefly. If they are all errors that I don't understand well, then the cost of understanding them is uncertain. In order to use this so-called PHPbrew to manage PHP versions, I would need to spend 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 directly switch the environment variables to the PHP version I need by using php_version xxx.

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 it is not known when they will refresh, it is best not to change the variables arbitrarily and just leave them fixed. So the core problem to be solved now is: how to change the actual PHP version used without changing the path?

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

Yes, 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 never thought about symbolic and hard links. When I was naive in college, when studying the operating system course, I thought symbolic links were just shortcuts, but there are still many differences...

Of course, this method has many limitations and is different from the true virtual environment in Python. After all, it is a global change. Now I have to add php_version php7.4 to every startup configuration to avoid the embarrassment of forgetting to change it back~

By the way, I recommend a plugin on Windows called "Link Shell Extension". After installing it, you can right-click on your files and quickly select them as the source, and then create symbolic or hard links to the location you want. It is 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.