Modifying Existing Projects

Change behavior, update UI, rename, restructure, swap libraries - safely. Includes 25+ ready-to-use prompts for every type of modification.

Intermediate 12 min read Lesson 13 of 20

The Three Rules

  1. Read before changing. Tell the AI to read the file(s) first, then make changes. "Read UserModel.php and then change the create() method to..."
  2. Say what must not change. "Change only the validation logic. Do not change the method signature or the database query."
  3. Review the diff. In Claude Code, see every change before accepting. In Cursor, review the diff view. Never accept blind changes to existing files.

Change Behavior

Behavior change prompts
 Read UserModel::create(). Change it so that:
  - Email must be unique (throw DuplicateEmailException if exists)
  - Name must be 2-100 characters
  - Phone number is optional but if provided must be 10 digits
  Change only the validation section, not the INSERT query.

# Change a calculation:
> In OrderModel::calculateTotal(), change the tax rate from
  18% to the value stored in config.php as TAX_RATE constant.
  Also add a 5% discount if the user has role 'premium'.

# Change how data is returned:
> Read ProductModel::findAll(). Change it to return products
  sorted by popularity (a 'views' column) by default instead of created_at.
  Add an optional $sortBy parameter ('popularity', 'price', 'newest').

# Change access control:
> Update AuthMiddleware::check() so that:
  - Public routes (/login, /register, /about) do not require authentication
  - /admin/* routes require role='admin'
  - All other routes require any authenticated user
  Read the current check() method first.

Change UI / Styling

UI change prompts
# Change the entire color scheme:
> Read assets/css/app.css. Change the color scheme from blue (#1a56db)
  to a green theme (#16a34a primary, #14532d dark).
  Update all CSS variables and any hard-coded hex values.

# Redesign a section:
> Read views/projects/index.php. Change the project listing from
  a card grid to a table view with columns:
  Project Name, Owner, Tasks (count), Status, Last Updated, Actions.
  Keep the existing PHP logic, only change the HTML.

# Add responsive behavior:
> Read views/admin/users.php. The table breaks on mobile.
  Make it responsive: on screens < 768px, hide the 'Created At' and
  'Last Login' columns, stack the Action buttons vertically.

# Update to a newer Bootstrap version:
> Read includes/header.php and all view files.
  Update from Bootstrap 4 to Bootstrap 5:
  - mr-/ml- -> me-/ms-
  - text-left -> text-start
  - data-toggle -> data-bs-toggle
  - data-target -> data-bs-target
  - Remove jQuery dependency from Bootstrap components

Rename and Move Code

Rename / move prompts
# Rename a method used everywhere:
> Rename UserModel::getUser() to UserModel::findById() across the entire codebase.
  Find every file that calls ->getUser( and update it.
  Do not change any other methods.

# Move logic to a service class:
> In AuthController, the processLogin() method has 60 lines of
  password verification and session management logic.
  Extract this to a new class AuthService::login(string $email, string $password, bool $remember): bool
  AuthController should call AuthService::login() with 3 lines of code.

# Move config to environment variables:
> Find every hardcoded database credential, API key, or URL in the codebase.
  Move them to a .env file and read them with getenv() in config/db.php and config/app.php.
  Create .env.example with placeholder values.
  Add .env to .gitignore.

Update Dependencies

Dependency update prompts
# Update a PHP package:
> Read composer.json and all files that use the PHPMailer library.
  Update from PHPMailer 5.x to PHPMailer 6.x.
  The API changed: list the specific method and property changes,
  then update all usages in the code.

# Replace jQuery with vanilla JS:
> Read assets/js/main.js. This file uses jQuery.
  Rewrite it to use vanilla JavaScript (no jQuery dependency).
  Focus on: DOM manipulation, event listeners, AJAX calls (use fetch()).

# Upgrade PHP version:
> Read all .php files and find any code that is deprecated in PHP 8.2:
  - ereg_replace (use preg_replace)
  - each() (use foreach)
  - create_function() (use arrow functions)
  - Old-style constructor (use __construct)
  List all issues found and fix them.

Replace a Component or Library

Component replacement prompts
# Replace file upload library:
> The app uses a custom file upload function in functions.php.
  Replace it with the Flysystem library (already in composer.json).
  Read the existing function first. Create a new FileUploadService class
  that uses Flysystem but exposes the same interface as the old function.
  Update all callers to use the new service.

# Replace email solution:
> The app uses PHP mail() for all emails. Read all files that call mail().
  Replace them to use Symfony Mailer (add to composer.json).
  Create an EmailService wrapper so callers do not need to know about Symfony Mailer.

# Replace in-memory session with Redis:
> The app uses PHP sessions ($_SESSION). Replace session storage with Redis
  using session_set_save_handler. Existing code that reads/writes $_SESSION
  should continue to work unchanged - only the storage backend changes.
  Add redis connection config to config/app.php.

Frequently Asked Questions

Never directly. The workflow is: (1) work on a copy or branch, (2) let AI make changes, (3) review the diff carefully, (4) test thoroughly, (5) then deploy. Claude Code shows you every file change before applying it - use that visibility. Never run AI modifications directly on a live server without this review process.

This is where Claude Code shines. "Find every file that uses the old database connection method and update it to use the new PDO wrapper." The agent will grep for all usages, read each file, make the appropriate change, and report what it changed. Batch changes that would take a human hours take Claude Code minutes.