My PHP test server is a MAMP installation. I recently decided to upgrade my Mac OS X to Catalina, and upgraded my MAMP installation to version 5.7. This allowed me to test my code on PHP 7.4.2, and since I use Phing, I needed to reinstall it on the new PHP 7.4.2 instance. I encountered an interesting error (or technically, a notice) that I found to be very annoying, and misleading. I want to share the fix with you, and go through the steps of installing Phing together.
Phing is installed using PEAR. The first thing you need to do is to navigate to the location of PEAR on your MAMP 5.7 installation on Mac OS X Catalina. We will be concerned with the location of the PEAR executable and also where the lib files are located.
Step 1 – Fix the REST.php file
Open a Finder window, and navigate to the following location: /Applications/MAMP/bin/php/php7.4.2/lib/php/PEAR. Open the REST.php file in your favorite text editor. Go to line 181, and make the following change:
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
function useLocalCache($url, $cacheid = null) { if (!is_array($cacheid)) { $cacheid = $this->getCacheId($url); } $cachettl = $this->config->get('cache_ttl'); // If cache is newer than $cachettl seconds, we use the cache! if ($cacheid && time() - $cacheid['age'] < $cachettl) {/* ADDED '$cacheid && ' to condition to handle when getCacheId returns false. */ return $this->getCache($url); } return false; } |
Basically, we are adding $cacheid && to the beginning of the if statement. This facilitates handling the use case of where the getCacheId function returns false when there is no cache file.
If you don’t patch the REST.php file, then you will get the following message when you install Phing from PEAR, and it will be repeated numerous times during the installation process:
1 |
Notice: Trying to access array offset on value of type bool in PEAR/REST.php on line 181 |
Step 2 – Update your PATH variable
Before we install and test Phing, let’s setup the Terminal shell to respond to typing “phing”. With Mac OS X Catalina, Apple has changed the default shell from bash to zsh. Adding the location of phing to either bash or zsh involves editing the .bash_profile or the .zshrc, respectively. The change we make to either of these files are exactly the same. Depending on which shell you use, you will choose which file to edit. Both of these files will be located in your user home directory. Open your Terminal app, and enter the following commend in your bash or zsh prompt to go to your home directory immediately:
1 |
cd ~ |
If you are using bash, edit the .bash_profile file:
1 |
vim .bash_profile |
If you are using zsh, edit the .zshrc file:
1 |
vim .zshrc |
In vim, press i to insert text in the file, and type the following:
1 2 3 |
PATH="$PATH:/Applications/MAMP/bin/php/php7.4.2/bin" export PATH |
To save the changed to the file. Press [ESC], then type :wq and press [ENTER].
Step 3 – Install Phing and Use it
Open a new Terminal window, and enter the following command in your bash or zsh prompt then press [ENTER]:
1 |
cd /Applications/MAMP/bin/php/php7.4.2/bin |
Type the command, “./pear channel-discover pear.phing.info” to make sure that PEAR can find the Phing package then press [ENTER]:
1 2 3 |
./pear channel-discover pear.phing.info Adding Channel "pear.phing.info" succeeded Discovery of channel "pear.phing.info" succeeded |
Once the Phing channel was successfully discovered, type “./pear install phing/phing” to install Phing:
1 |
./pear install phing/phing |
After Phing is installed, you will see massage that confirms a successful installation:
1 |
install ok: channel://pear.phing.info/phing-2.16.1 |
Lastly, open a new Terminal window, and type “phing -v” to test your setup. It should display the version of Phing that was installed:
1 2 |
phing -v Phing 2.16.1 |