Home  >  Article  >  Development Tools  >  What is the difference between Composer’s tilde symbol ~ and the power symbol ^

What is the difference between Composer’s tilde symbol ~ and the power symbol ^

藏色散人
藏色散人forward
2020-08-12 13:16:553997browse

The following tutorial column of composer will introduce to you the difference between Composer’s tilde symbol ~ and the power symbol ^. I hope it will be helpful to friends in need!

What is the difference between Composer’s tilde symbol ~ and the power symbol ^

I have seen too many people writing version numbers randomly (in fact, it is much better now than before). The next version of 1.9.0 must be 2.0.0, 2.9 .0 The next version must be 3.0.0... Mainly because there was no standard naming of version numbers before, and everyone wrote according to their mood. Later, semantic versioning was promoted, and the situation of version number naming was improved, but there are still many people who don’t know what the three-segment version number means (will you die if you teach these specifications in various training schools...).

To put it simply (I am just moving it, the website mentioned above makes it clearer), the meaning of each segment of the three-segment version number is:

Large version. Small version. Patch version

Let’s talk about the most obvious patch version first. If your software or library has had some bugs fixed before, the patch version needs to be 1.

Let’s talk about the minor version number. If you add a new feature, such as an RPG game that automatically picks up things, or a new method in your code base, it will not cause compatibility issues (BC) if users upgrade your code base, so a small version No. 1.

Finally, if your software is completely refactored, or your code base is completely refactored so that users upgrading the new library will inevitably cause compatibility issues, then the major version number is 1.

Of course, there will be cases where bugs are modified and functions are added at the same time. In short, if the previous bit requires 1, the following bits must be cleared to zero in any case.

In fact, there is no need to be too strict about software for end users, because end users don’t care much about the version (except for the big version number..., so chrome-style version numbers appeared, and Firefox also followed suit. ...), but for library file authors, it is better to abide by the version number, because the version number you set provides very important information to library callers, and they need to use your version number to determine whether to avoid compatibility issues. .

To give a more classic example, although Symfony 2/3/4 are similar in use, because there are interface inconsistencies between these three versions, the major version number must be 1. If your library file has an interface change that makes it incompatible with previous versions, even if there is only one, you should change the major version number to 1.

And if you develop some projects using Symfony 3.0.x, even if you upgrade directly to 3.4.x, theoretically there is no problem at all.

After talking about the three-segment version number, let’s talk about a problem that troubles PHPer. What do the ^ symbols and ~ symbols that specify the version number in composer.json mean? What's the difference?

Before writing this blog, I actually searched on Baidu and wanted to know if there were many related articles and whether this article was worth writing. The search results show that someone has indeed summarized it. Although most of them only have one example:

~1.2.3 represents 1.2.3 <= version number< 1.3.0

^1.2.3 represents 1.2.3 <= version number< ; 2.0.0

Based on the above transfer content, I will add a little more, ~ means that the version number can only change the last paragraph (if it is ~x.y, the end is y, if it is ~x.y.z, the end is z), For example, in this case:

~1.2 is equal to >=1.2.0; <2.0.0

and the ^ symbol is related to the semantic versioning mentioned above. It means to try to use the latest version, but to ensure that no compatibility problems occur. In other words, in addition to the major version number, the minor version number and the patch version number can be changed.

But it seems that there is a very important detail that no one has mentioned yet. In semantic versioning, the version number starting with 0 is quite special, and because ^ strictly abides by the semantic versioning rules, the following will appear. Situation (earlier composer versions did not have this processing, but it was later corrected):

^0.3.0 is equal to>=0.3.0; <0.4.0

eh? Shouldn't it be <1.0.0?

The reason is that the rule of semantic versioning is that if the major version number starts with 0, it means that it is an unstable version (unstable). If it is in an unstable state, the minor version number is not allowed to be backward compatible! So if you want to specify a library starting with 0, you must pay attention:

~0.1 This way of writing is very dangerous, because ~0.1 is equal to >=0.1.0; <1.0.0, which may cause failure In the case of backward compatibility, the safer way to write it is

^0.1 (equal to >=0.1.0; <0.2.0)

The above is the detailed content of What is the difference between Composer’s tilde symbol ~ and the power symbol ^. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:chrisyue.com. If there is any infringement, please contact admin@php.cn delete