Here’s how to fix “Fatal error: Cannot declare class” in PHP Composer

Fix “Fatal error: Cannot declare class” in PHP Composer

I ran into a small problem while trying to install packages with PHP Composer. The error message didn’t help me. Here’s how you can fix it in under one minute.

The Problem

I’m currently developing a WordPress site that uses Bedrock by roots.io. Since it comes with a handy composer.json file, I needed to run composer install in the CLI. But when I did, the following shortened error message was returned:

Fatal error: Cannot declare class pxn\ComposerLocalDev\LocalDev_Config, because the name is already in use (…)

I’ve searched the project and other folders for this LocalDev_Config but couldn’t find anything. A Google search brought me to a StackOverflow question whose answer didn’t help me; Bedrock’s composer.json doesn’t contain any class names or namespaces. Llama 3.2 90B via Groq gave me a long answer that I didn’t even bother to read.

The Fix

Since PHP Composer is just a tool and doesn’t store any very important data, deleting it fixed the problem. To get rid of it and reinstall it freshly, I used the following commands with Homebrew for macOS:

# Uninstall PHP Composer with Homebrew
brew uninstall composer

# Delete the Composer config directory in your home directory
rm -r ~/.composer

# Use Homebrew's `cleanup` command to, well, clean things up if necessary
brew cleanup

# Install PHP Composer again
brew install composer
Bash

After that, everything went smoothly again.