For one of my projects I use the following script to reset my development environment database when switching branches:
php bin/console doctrine:database:drop --force
php bin/console doctrine:database:create -n
php bin/console doctrine:database:import dump.sql
php bin/console doctrine:migrations:migrate --no-interaction
Simple enough. The problem? When running it, the default php binary installed with Mac is used. As of time of writing this is 7.3. But of course I want to use the property typing introduced by 7.4 (not to mention that the project is on 7.4 in general). The solution: Aliases
When using MAMP Pro, I just add the following alias to my bash (
.bashrc) or zsh (
alias php='/Applications/MAMP/bin/php/php7.4.2/bin/php -c /Applications/MAMP/bin/php/php7.4.2/conf/php.ini'
This way whenever I use
php it will always use the 7.4 version.
When running the script I will receive errors because 7.3 is used. Why? Because when executing a shell script, it will be run in a new shell process. And this process doesn't have access to the aliases defined in the configuration.
But we can use a trick called "sourcing". When sourcing a shell script, it will run in the current process and therefore have access to the aliases. To do so we use the
That's it. This way you can use aliases in scripts and switch PHP versions between projects just by switching the alias definitions.